Skip to content

Commit fe90c82

Browse files
authored
Merge branch 'main' into bobby/plat-664-dcr-scope-warning
2 parents 488ca37 + fd5b375 commit fe90c82

14 files changed

Lines changed: 219 additions & 36 deletions

‎coderd/database/dbgen/dbgen.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func ChatMessage(t testing.TB, db database.Store, seed database.ChatMessage) dat
141141
ContextLimit: []int64{seed.ContextLimit.Int64},
142142
Compressed: []bool{seed.Compressed},
143143
RuntimeMs: []int64{seed.RuntimeMs.Int64},
144+
ProviderResponseID: []string{seed.ProviderResponseID.String},
144145
})
145146
require.NoError(t, err, "insert chat message")
146147
require.Len(t, msgs, 1)

‎coderd/database/queries.sql.go‎

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎coderd/database/queries/chats.sql‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ inserted AS (
954954
cache_read_tokens,
955955
context_limit,
956956
compressed,
957-
runtime_ms
957+
runtime_ms,
958+
provider_response_id
958959
)
959960
SELECT
960961
allocated.id,
@@ -974,7 +975,8 @@ inserted AS (
974975
NULLIF((@cache_read_tokens::bigint[])[allocated.ord], 0),
975976
NULLIF((@context_limit::bigint[])[allocated.ord], 0),
976977
(@compressed::boolean[])[allocated.ord],
977-
NULLIF((@runtime_ms::bigint[])[allocated.ord], 0)
978+
NULLIF((@runtime_ms::bigint[])[allocated.ord], 0),
979+
NULLIF((@provider_response_id::text[])[allocated.ord], '')
978980
FROM allocated
979981
RETURNING *
980982
)

‎coderd/x/chatd/attempt.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const (
2020

2121
// stepData is the durable content produced by one provider attempt.
2222
type stepData struct {
23-
Content []fantasy.Content
24-
Usage fantasy.Usage
25-
ContextLimit sql.NullInt64
26-
Runtime time.Duration
23+
Content []fantasy.Content
24+
Usage fantasy.Usage
25+
ContextLimit sql.NullInt64
26+
Runtime time.Duration
27+
ProviderResponseID string
2728

2829
// BatchRuntime is the local-tool batch window. Model steps use Runtime.
2930
BatchRuntime time.Duration
@@ -57,6 +58,7 @@ type compactionOutcome struct {
5758
ContextLimit int64
5859
EstimatedContextTokens int64
5960
Runtime time.Duration
61+
ProviderResponseID string
6062
}
6163

6264
type compactionStatus int

‎coderd/x/chatd/chatd_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5263,6 +5263,37 @@ func readFileResponseWithInputTokens(path string, inputTokens int) chattest.Anth
52635263
return chattest.AnthropicStreamingResponse(chunks...)
52645264
}
52655265

5266+
func TestActiveServer_PersistsAnthropicMessageID(t *testing.T) {
5267+
t.Parallel()
5268+
5269+
ctx := testutil.Context(t, testutil.WaitLong)
5270+
db, ps := dbtestutil.NewDB(t)
5271+
anthropicURL := chattest.NewAnthropic(t, func(req *chattest.AnthropicRequest) chattest.AnthropicResponse {
5272+
if !req.Stream {
5273+
return chattest.AnthropicNonStreamingResponse("title")
5274+
}
5275+
chunks := chattest.AnthropicTextChunks("answer")
5276+
chunks[0].Message.ID = "msg_persisted"
5277+
return chattest.AnthropicStreamingResponse(chunks...)
5278+
})
5279+
user, org, model := seedAnthropicChatDependencies(t, db, anthropicURL)
5280+
server := newActiveTestServer(t, db, ps, func(cfg *chatd.Config) {
5281+
cfg.AIBridgeTransportFactory = chatAIGatewayTransportFactoryPointer(chattest.NewMockAIBridgeTransport(t, anthropicURL, chattest.WithPreservePath()))
5282+
})
5283+
5284+
chat := createChatThroughServer(ctx, t, db, server, org.ID, user.ID, model.ID, "hello")
5285+
waitForChatStatus(ctx, t, db, chat.ID, database.ChatStatusWaiting)
5286+
5287+
messages, err := db.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{ChatID: chat.ID})
5288+
require.NoError(t, err)
5289+
responseIDs := make(map[database.ChatMessageRole]sql.NullString)
5290+
for _, message := range messages {
5291+
responseIDs[message.Role] = message.ProviderResponseID
5292+
}
5293+
require.Equal(t, sql.NullString{String: "msg_persisted", Valid: true}, responseIDs[database.ChatMessageRoleAssistant])
5294+
require.False(t, responseIDs[database.ChatMessageRoleUser].Valid)
5295+
}
5296+
52665297
func TestActiveServer_RoutingPreservesAPIKeyAfterCompaction(t *testing.T) {
52675298
t.Parallel()
52685299

‎coderd/x/chatd/chatloop/chatloop.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"charm.land/fantasy"
1919
fantasyanthropic "charm.land/fantasy/providers/anthropic"
20+
fantasyopenai "charm.land/fantasy/providers/openai"
2021
"github.com/google/uuid"
2122
"golang.org/x/xerrors"
2223

@@ -66,6 +67,10 @@ type PersistedStep struct {
6667
// Runtime is the wall-clock duration from opening to consuming the
6768
// model stream.
6869
Runtime time.Duration
70+
// ProviderResponseID is the response ID the model endpoint reported, if
71+
// any. Through the AI Gateway, Anthropic Messages responses carry the
72+
// gateway's interception ID instead of the upstream message ID.
73+
ProviderResponseID string
6974
// BatchRuntime is the union of billed local-tool execution intervals.
7075
// Parallel calls count once and serial calls count from their own start.
7176
BatchRuntime time.Duration
@@ -263,6 +268,7 @@ type stepResult struct {
263268
content []fantasy.Content
264269
usage fantasy.Usage
265270
providerMetadata fantasy.ProviderMetadata
271+
providerResponseID string
266272
finishReason fantasy.FinishReason
267273
toolCalls []fantasy.ToolCallContent
268274
toolCallCreatedAt map[string]time.Time
@@ -380,6 +386,7 @@ func GenerateAssistant(ctx context.Context, opts GenerateAssistantOptions) (Assi
380386
Usage: result.usage,
381387
ContextLimit: contextLimit,
382388
Runtime: opts.Clock.Since(stepStart),
389+
ProviderResponseID: result.providerResponseID,
383390
ToolCallCreatedAt: result.toolCallCreatedAt,
384391
ToolResultCreatedAt: result.toolResultCreatedAt,
385392
ReasoningStartedAt: result.reasoningStartedAt,
@@ -999,6 +1006,7 @@ func processStepStream(
9991006
result.usage = part.Usage
10001007
result.finishReason = part.FinishReason
10011008
result.providerMetadata = part.ProviderMetadata
1009+
result.providerResponseID = providerResponseID(part)
10021010

10031011
case fantasy.StreamPartTypeError:
10041012
return result, part.Error
@@ -1008,6 +1016,22 @@ func processStepStream(
10081016
return result, nil
10091017
}
10101018

1019+
// providerResponseID returns the response ID from a finish part.
1020+
// Anthropic and Bedrock report the message ID as the part ID, and the OpenAI
1021+
// Responses API reports it in provider metadata. Chat Completions providers
1022+
// report neither.
1023+
func providerResponseID(part fantasy.StreamPart) string {
1024+
if part.ID != "" {
1025+
return part.ID
1026+
}
1027+
for _, metadata := range part.ProviderMetadata {
1028+
if responses, ok := metadata.(*fantasyopenai.ResponsesProviderMetadata); ok && responses != nil {
1029+
return responses.ResponseID
1030+
}
1031+
}
1032+
return ""
1033+
}
1034+
10111035
type toolExecutionResult struct {
10121036
content fantasy.ToolResultContent
10131037
interval BilledInterval

‎coderd/x/chatd/chatloop/chatloop_run_internal_test.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"charm.land/fantasy"
2020
fantasyanthropic "charm.land/fantasy/providers/anthropic"
21+
fantasyopenai "charm.land/fantasy/providers/openai"
2122
"github.com/prometheus/client_golang/prometheus"
2223
promtestutil "github.com/prometheus/client_golang/prometheus/testutil"
2324
"github.com/stretchr/testify/assert"
@@ -176,6 +177,60 @@ func TestGenerateAssistant_ProviderContextSurvivesStreamError(t *testing.T) {
176177
require.Equal(t, "OpenAI returned an unexpected error.", classified.Message)
177178
}
178179

180+
func TestGenerateAssistant_ProviderResponseID(t *testing.T) {
181+
t.Parallel()
182+
183+
cases := []struct {
184+
name string
185+
finish fantasy.StreamPart
186+
want string
187+
}{
188+
{
189+
name: "anthropic message ID",
190+
finish: fantasy.StreamPart{ID: "msg_anthropic"},
191+
want: "msg_anthropic",
192+
},
193+
{
194+
name: "openai responses metadata",
195+
finish: fantasy.StreamPart{ProviderMetadata: fantasy.ProviderMetadata{
196+
fantasyopenai.Name: &fantasyopenai.ResponsesProviderMetadata{ResponseID: "resp_openai"},
197+
}},
198+
want: "resp_openai",
199+
},
200+
{
201+
name: "not reported",
202+
},
203+
}
204+
for _, tc := range cases {
205+
t.Run(tc.name, func(t *testing.T) {
206+
t.Parallel()
207+
208+
finish := tc.finish
209+
finish.Type = fantasy.StreamPartTypeFinish
210+
finish.FinishReason = fantasy.FinishReasonStop
211+
model := &chattest.FakeModel{
212+
ProviderName: "fake",
213+
ModelName: "fake-model",
214+
StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) {
215+
return streamFromParts([]fantasy.StreamPart{
216+
{Type: fantasy.StreamPartTypeTextStart, ID: "text"},
217+
{Type: fantasy.StreamPartTypeTextDelta, ID: "text", Delta: "hi"},
218+
{Type: fantasy.StreamPartTypeTextEnd, ID: "text"},
219+
finish,
220+
}), nil
221+
},
222+
}
223+
224+
outcome, err := GenerateAssistant(context.Background(), GenerateAssistantOptions{
225+
Model: model,
226+
Messages: []fantasy.Message{textMessage(fantasy.MessageRoleUser, "hello")},
227+
})
228+
require.NoError(t, err)
229+
require.Equal(t, tc.want, outcome.Step.ProviderResponseID)
230+
})
231+
}
232+
}
233+
179234
func TestGenerateAssistant_ErrorProviderOverridesTransportLabel(t *testing.T) {
180235
t.Parallel()
181236

‎coderd/x/chatd/chatloop/compaction.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ type CompactionResult struct {
143143
// PersistedStep.Runtime). Zero when the run was gated off before
144144
// calling the model.
145145
Runtime time.Duration
146+
// ProviderResponseID identifies the summary response. See
147+
// PersistedStep.ProviderResponseID.
148+
ProviderResponseID string
146149
}
147150

148151
// GenerateCompaction generates one context summary and returns it without
@@ -207,7 +210,7 @@ func GenerateCompaction(ctx context.Context, opts GenerateCompactionOptions) (Co
207210
if opts.OnModelStreamStart != nil {
208211
opts.OnModelStreamStart()
209212
}
210-
summary, err := generateCompactionSummary(ctx, opts.Model, opts.Messages, config)
213+
summary, responseID, err := generateCompactionSummary(ctx, opts.Model, opts.Messages, config)
211214
if err != nil {
212215
publishCompactionError(config, "failed to generate compaction summary")
213216
return CompactionResult{}, err
@@ -222,13 +225,14 @@ func GenerateCompaction(ctx context.Context, opts GenerateCompactionOptions) (Co
222225
SystemSummary: strings.TrimSpace(
223226
config.SystemSummaryPrefix + "\n\n" + summary,
224227
),
225-
SummaryReport: summary,
226-
Source: config.Source,
227-
ThresholdPercent: config.ThresholdPercent,
228-
UsagePercent: usagePercent,
229-
ContextTokens: contextTokens,
230-
ContextLimit: contextLimit,
231-
Runtime: summaryRuntime,
228+
SummaryReport: summary,
229+
Source: config.Source,
230+
ThresholdPercent: config.ThresholdPercent,
231+
UsagePercent: usagePercent,
232+
ContextTokens: contextTokens,
233+
ContextLimit: contextLimit,
234+
Runtime: summaryRuntime,
235+
ProviderResponseID: responseID,
232236
}
233237
result.EstimatedContextTokens = int64((len(result.SystemSummary) + bytesPerTokenEstimate - 1) / bytesPerTokenEstimate)
234238
if config.PublishMessagePart != nil && config.ToolCallID != "" {
@@ -481,7 +485,7 @@ func generateCompactionSummary(
481485
model fantasy.LanguageModel,
482486
messages []fantasy.Message,
483487
options CompactionOptions,
484-
) (summary string, err error) {
488+
) (summary string, responseID string, err error) {
485489
summaryPrompt := make([]fantasy.Message, 0, len(messages)+1)
486490
summaryPrompt = append(summaryPrompt, messages...)
487491
summaryParts := []fantasy.MessagePart{fantasy.TextPart{Text: options.SummaryPrompt}}
@@ -575,6 +579,7 @@ func generateCompactionSummary(
575579
case fantasy.StreamPartTypeFinish:
576580
finishSeen = true
577581
finishReason = part.FinishReason
582+
responseID = providerResponseID(part)
578583
case fantasy.StreamPartTypeError:
579584
streamErr = part.Error
580585
if streamErr == nil {
@@ -626,7 +631,7 @@ func generateCompactionSummary(
626631
call.Tools = nil
627632
summary, err = streamSummaryText()
628633
}
629-
return summary, err
634+
return summary, responseID, err
630635
}
631636

632637
// contextTooLargePhrases are context-window rejections fantasy does not

0 commit comments

Comments
 (0)