Changeset 0.17.1 (#76)

This commit is contained in:
2026-02-28 01:40:31 +00:00
parent c9141a6896
commit 856dc9b0ac
64 changed files with 8037 additions and 1657 deletions

View File

@@ -647,7 +647,7 @@ func (h *CompletionHandler) resolveConfig(userID string, channelID string, req c
if configID == "" {
var channelConfigID *string
err := database.DB.QueryRow(
`SELECT provider_config_id FROM channels WHERE id = $1`, channelID,
database.Q(`SELECT provider_config_id FROM channels WHERE id = $1`), channelID,
).Scan(&channelConfigID)
if err == nil && channelConfigID != nil {
configID = *channelConfigID
@@ -656,7 +656,7 @@ func (h *CompletionHandler) resolveConfig(userID string, channelID string, req c
// 3. User's first active config (personal first, then global — excludes team providers)
if configID == "" {
err := database.DB.QueryRow(`
err := database.DB.QueryRow(database.Q(`
SELECT id FROM provider_configs
WHERE is_active = true AND (
(scope = 'personal' AND owner_id = $1)
@@ -664,7 +664,7 @@ func (h *CompletionHandler) resolveConfig(userID string, channelID string, req c
)
ORDER BY scope ASC, created_at ASC
LIMIT 1
`, userID).Scan(&configID)
`), userID).Scan(&configID)
if err != nil {
return providers.ProviderConfig{}, "", "", "", "", fmt.Errorf("no API config found — add one at /api-configs")
}
@@ -677,7 +677,13 @@ func (h *CompletionHandler) resolveConfig(userID string, channelID string, req c
var apiKeyEnc, keyNonce []byte
var keyScope string
var customHeadersJSON, providerSettingsJSON []byte
err := database.DB.QueryRow(`
// $2/userID appears twice in the query; Postgres reuses positional params,
// SQLite needs each ? bound separately.
configArgs := []interface{}{configID, userID}
if database.IsSQLite() {
configArgs = append(configArgs, userID)
}
err := database.DB.QueryRow(database.Q(`
SELECT provider, endpoint, scope, api_key_enc, key_nonce, key_scope,
model_default, headers, settings
FROM provider_configs
@@ -685,7 +691,7 @@ func (h *CompletionHandler) resolveConfig(userID string, channelID string, req c
AND (scope = 'global'
OR (scope = 'personal' AND owner_id = $2)
OR (scope = 'team' AND owner_id IN (SELECT team_id FROM team_members WHERE user_id = $2)))
`, configID, userID).Scan(&providerID, &endpoint, &providerScope, &apiKeyEnc, &keyNonce, &keyScope,
`), configArgs...).Scan(&providerID, &endpoint, &providerScope, &apiKeyEnc, &keyNonce, &keyScope,
&modelDefault, &customHeadersJSON, &providerSettingsJSON)
if err == sql.ErrNoRows {
@@ -767,7 +773,7 @@ func (h *CompletionHandler) loadConversation(channelID, userID, presetSystemProm
// ── User/preset system prompt (appended after admin prompt) ──
var systemPrompt *string
_ = database.DB.QueryRow(
`SELECT system_prompt FROM channels WHERE id = $1`, channelID,
database.Q(`SELECT system_prompt FROM channels WHERE id = $1`), channelID,
).Scan(&systemPrompt)
// Preset system prompt takes priority; channel system prompt is fallback
@@ -882,18 +888,31 @@ func (h *CompletionHandler) persistMessage(channelID, userID, role, content, mod
tcVal = string(toolCallsJSON)
}
// Insert with RETURNING id
// Insert with RETURNING id (Postgres) or pre-generated id (SQLite)
var newID string
err := database.DB.QueryRow(`
INSERT INTO messages (channel_id, role, content, model, tokens_used,
tool_calls, parent_id, participant_type, participant_id, sibling_index)
VALUES ($1, $2, $3, NULLIF($4, ''), $5, $6, $7, $8, $9, $10)
RETURNING id
`, channelID, role, content, model, tokensUsed,
tcVal, parentID, participantType, participantID, siblingIdx,
).Scan(&newID)
if err != nil {
return "", err
if database.IsSQLite() {
newID = store.NewID()
_, err := database.DB.Exec(`
INSERT INTO messages (id, channel_id, role, content, model, tokens_used,
tool_calls, parent_id, participant_type, participant_id, sibling_index)
VALUES (?, ?, ?, ?, NULLIF(?, ''), ?, ?, ?, ?, ?, ?)
`, newID, channelID, role, content, model, tokensUsed,
tcVal, parentID, participantType, participantID, siblingIdx)
if err != nil {
return "", err
}
} else {
err := database.DB.QueryRow(`
INSERT INTO messages (channel_id, role, content, model, tokens_used,
tool_calls, parent_id, participant_type, participant_id, sibling_index)
VALUES ($1, $2, $3, NULLIF($4, ''), $5, $6, $7, $8, $9, $10)
RETURNING id
`, channelID, role, content, model, tokensUsed,
tcVal, parentID, participantType, participantID, siblingIdx,
).Scan(&newID)
if err != nil {
return "", err
}
}
// Update cursor to point at new message
@@ -902,7 +921,7 @@ func (h *CompletionHandler) persistMessage(channelID, userID, role, content, mod
}
// Touch channel updated_at
_, _ = database.DB.Exec(`UPDATE channels SET updated_at = NOW() WHERE id = $1`, channelID)
_, _ = database.DB.Exec(database.Q(`UPDATE channels SET updated_at = NOW() WHERE id = $1`), channelID)
return newID, nil
}