Files
simstudioai--sim/apps/sim/lib/copilot/chat/effective-transcript.test.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

264 lines
7.3 KiB
TypeScript

/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import {
buildEffectiveChatTranscript,
getLiveAssistantMessageId,
} from '@/lib/copilot/chat/effective-transcript'
import { normalizeMessage } from '@/lib/copilot/chat/persisted-message'
import {
MothershipStreamV1CompletionStatus,
MothershipStreamV1EventType,
MothershipStreamV1SessionKind,
MothershipStreamV1TextChannel,
} from '@/lib/copilot/generated/mothership-stream-v1'
import type { StreamBatchEvent } from '@/lib/copilot/request/session/types'
function toBatchEvent(eventId: number, event: StreamBatchEvent['event']): StreamBatchEvent {
return {
eventId,
streamId: event.stream.streamId,
event,
}
}
function buildUserMessage(id: string, content: string) {
return normalizeMessage({
id,
role: 'user',
content,
timestamp: '2026-04-15T12:00:00.000Z',
})
}
describe('buildEffectiveChatTranscript', () => {
it('returns the existing transcript when the stream owner is no longer the trailing user', () => {
const messages = [
buildUserMessage('stream-1', 'Hello'),
normalizeMessage({
id: 'assistant-1',
role: 'assistant',
content: 'Persisted response',
timestamp: '2026-04-15T12:00:01.000Z',
}),
]
const result = buildEffectiveChatTranscript({
messages,
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.text,
stream: { streamId: 'stream-1' },
payload: {
channel: MothershipStreamV1TextChannel.assistant,
text: 'Live response',
},
}),
],
previewSessions: [],
status: 'active',
},
})
expect(result).toEqual(messages)
})
it('appends a placeholder assistant while an active stream has not produced text yet', () => {
const result = buildEffectiveChatTranscript({
messages: [buildUserMessage('stream-1', 'Hello')],
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.session,
stream: { streamId: 'stream-1' },
payload: {
kind: MothershipStreamV1SessionKind.start,
},
}),
],
previewSessions: [],
status: 'active',
},
})
expect(result).toHaveLength(2)
expect(result[1]).toEqual(
expect.objectContaining({
id: getLiveAssistantMessageId('stream-1'),
role: 'assistant',
content: '',
})
)
})
it('materializes a live assistant response from redis-backed stream events', () => {
const result = buildEffectiveChatTranscript({
messages: [buildUserMessage('stream-1', 'Hello')],
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.session,
stream: { streamId: 'stream-1' },
trace: { requestId: 'req-1' },
payload: {
kind: MothershipStreamV1SessionKind.trace,
requestId: 'req-1',
},
}),
toBatchEvent(2, {
v: 1,
seq: 2,
ts: '2026-04-15T12:00:02.000Z',
type: MothershipStreamV1EventType.text,
stream: { streamId: 'stream-1' },
trace: { requestId: 'req-1' },
payload: {
channel: MothershipStreamV1TextChannel.assistant,
text: 'Live response',
},
}),
],
previewSessions: [],
status: 'active',
},
})
expect(result).toHaveLength(2)
expect(result[1]).toEqual(
expect.objectContaining({
id: getLiveAssistantMessageId('stream-1'),
role: 'assistant',
content: 'Live response',
requestId: 'req-1',
})
)
})
it('does not duplicate thinking-only text into a second assistant block', () => {
const result = buildEffectiveChatTranscript({
messages: [buildUserMessage('stream-1', 'Hello')],
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.text,
stream: { streamId: 'stream-1' },
payload: {
channel: MothershipStreamV1TextChannel.thinking,
text: 'Internal reasoning',
},
}),
],
previewSessions: [],
status: 'active',
},
})
expect(result).toHaveLength(2)
expect(result[1]).toEqual(
expect.objectContaining({
content: 'Internal reasoning',
contentBlocks: [
expect.objectContaining({
type: MothershipStreamV1EventType.text,
content: 'Internal reasoning',
}),
],
})
)
})
it('treats user-cancelled tool results as cancelled', () => {
const result = buildEffectiveChatTranscript({
messages: [buildUserMessage('stream-1', 'Hello')],
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.tool,
stream: { streamId: 'stream-1' },
payload: {
phase: 'result',
toolCallId: 'tool-1',
toolName: 'workspace_file',
executor: 'go',
mode: 'sync',
success: false,
output: {
reason: 'user_cancelled',
},
},
}),
],
previewSessions: [],
status: 'active',
},
})
expect(result[1]?.contentBlocks).toEqual([
expect.objectContaining({
type: MothershipStreamV1EventType.tool,
toolCall: expect.objectContaining({
id: 'tool-1',
name: 'workspace_file',
state: MothershipStreamV1CompletionStatus.cancelled,
}),
}),
])
})
it('materializes a cancelled assistant tail when the stream ends before persistence', () => {
const result = buildEffectiveChatTranscript({
messages: [buildUserMessage('stream-1', 'Hello')],
activeStreamId: 'stream-1',
streamSnapshot: {
events: [
toBatchEvent(1, {
v: 1,
seq: 1,
ts: '2026-04-15T12:00:01.000Z',
type: MothershipStreamV1EventType.complete,
stream: { streamId: 'stream-1' },
payload: {
status: MothershipStreamV1CompletionStatus.cancelled,
},
}),
],
previewSessions: [],
status: MothershipStreamV1CompletionStatus.cancelled,
},
})
expect(result).toHaveLength(2)
expect(result[1]?.contentBlocks).toEqual(
expect.arrayContaining([
expect.objectContaining({
type: MothershipStreamV1EventType.complete,
status: MothershipStreamV1CompletionStatus.cancelled,
}),
])
)
})
})