ai 6.0.33 → 6.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/02-foundations/03-prompts.mdx +2 -2
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +1 -1
- package/package.json +6 -4
- package/src/agent/agent.ts +116 -0
- package/src/agent/create-agent-ui-stream-response.test.ts +258 -0
- package/src/agent/create-agent-ui-stream-response.ts +50 -0
- package/src/agent/create-agent-ui-stream.ts +73 -0
- package/src/agent/index.ts +33 -0
- package/src/agent/infer-agent-tools.ts +7 -0
- package/src/agent/infer-agent-ui-message.test-d.ts +54 -0
- package/src/agent/infer-agent-ui-message.ts +11 -0
- package/src/agent/pipe-agent-ui-stream-to-response.ts +52 -0
- package/src/agent/tool-loop-agent-on-finish-callback.ts +31 -0
- package/src/agent/tool-loop-agent-on-step-finish-callback.ts +11 -0
- package/src/agent/tool-loop-agent-settings.ts +182 -0
- package/src/agent/tool-loop-agent.test-d.ts +114 -0
- package/src/agent/tool-loop-agent.test.ts +442 -0
- package/src/agent/tool-loop-agent.ts +114 -0
- package/src/embed/__snapshots__/embed-many.test.ts.snap +191 -0
- package/src/embed/__snapshots__/embed.test.ts.snap +81 -0
- package/src/embed/embed-many-result.ts +53 -0
- package/src/embed/embed-many.test.ts +653 -0
- package/src/embed/embed-many.ts +378 -0
- package/src/embed/embed-result.ts +50 -0
- package/src/embed/embed.test.ts +298 -0
- package/src/embed/embed.ts +211 -0
- package/src/embed/index.ts +4 -0
- package/src/error/index.ts +34 -0
- package/src/error/invalid-argument-error.ts +34 -0
- package/src/error/invalid-stream-part-error.ts +28 -0
- package/src/error/invalid-tool-approval-error.ts +26 -0
- package/src/error/invalid-tool-input-error.ts +33 -0
- package/src/error/no-image-generated-error.ts +39 -0
- package/src/error/no-object-generated-error.ts +70 -0
- package/src/error/no-output-generated-error.ts +26 -0
- package/src/error/no-speech-generated-error.ts +18 -0
- package/src/error/no-such-tool-error.ts +35 -0
- package/src/error/no-transcript-generated-error.ts +20 -0
- package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
- package/src/error/tool-call-repair-error.ts +30 -0
- package/src/error/unsupported-model-version-error.ts +23 -0
- package/src/error/verify-no-object-generated-error.ts +27 -0
- package/src/generate-image/generate-image-result.ts +42 -0
- package/src/generate-image/generate-image.test.ts +1420 -0
- package/src/generate-image/generate-image.ts +360 -0
- package/src/generate-image/index.ts +18 -0
- package/src/generate-object/__snapshots__/generate-object.test.ts.snap +133 -0
- package/src/generate-object/__snapshots__/stream-object.test.ts.snap +297 -0
- package/src/generate-object/generate-object-result.ts +67 -0
- package/src/generate-object/generate-object.test-d.ts +49 -0
- package/src/generate-object/generate-object.test.ts +1191 -0
- package/src/generate-object/generate-object.ts +518 -0
- package/src/generate-object/index.ts +9 -0
- package/src/generate-object/inject-json-instruction.test.ts +181 -0
- package/src/generate-object/inject-json-instruction.ts +30 -0
- package/src/generate-object/output-strategy.ts +415 -0
- package/src/generate-object/parse-and-validate-object-result.ts +111 -0
- package/src/generate-object/repair-text.ts +12 -0
- package/src/generate-object/stream-object-result.ts +120 -0
- package/src/generate-object/stream-object.test-d.ts +74 -0
- package/src/generate-object/stream-object.test.ts +1950 -0
- package/src/generate-object/stream-object.ts +986 -0
- package/src/generate-object/validate-object-generation-input.ts +144 -0
- package/src/generate-speech/generate-speech-result.ts +30 -0
- package/src/generate-speech/generate-speech.test.ts +300 -0
- package/src/generate-speech/generate-speech.ts +190 -0
- package/src/generate-speech/generated-audio-file.ts +65 -0
- package/src/generate-speech/index.ts +3 -0
- package/src/generate-text/__snapshots__/generate-text.test.ts.snap +1872 -0
- package/src/generate-text/__snapshots__/stream-text.test.ts.snap +1255 -0
- package/src/generate-text/collect-tool-approvals.test.ts +553 -0
- package/src/generate-text/collect-tool-approvals.ts +116 -0
- package/src/generate-text/content-part.ts +25 -0
- package/src/generate-text/execute-tool-call.ts +129 -0
- package/src/generate-text/extract-reasoning-content.ts +17 -0
- package/src/generate-text/extract-text-content.ts +15 -0
- package/src/generate-text/generate-text-result.ts +168 -0
- package/src/generate-text/generate-text.test-d.ts +68 -0
- package/src/generate-text/generate-text.test.ts +7011 -0
- package/src/generate-text/generate-text.ts +1223 -0
- package/src/generate-text/generated-file.ts +70 -0
- package/src/generate-text/index.ts +57 -0
- package/src/generate-text/is-approval-needed.ts +29 -0
- package/src/generate-text/output-utils.ts +23 -0
- package/src/generate-text/output.test.ts +698 -0
- package/src/generate-text/output.ts +590 -0
- package/src/generate-text/parse-tool-call.test.ts +570 -0
- package/src/generate-text/parse-tool-call.ts +188 -0
- package/src/generate-text/prepare-step.ts +103 -0
- package/src/generate-text/prune-messages.test.ts +720 -0
- package/src/generate-text/prune-messages.ts +167 -0
- package/src/generate-text/reasoning-output.ts +20 -0
- package/src/generate-text/reasoning.ts +8 -0
- package/src/generate-text/response-message.ts +10 -0
- package/src/generate-text/run-tools-transformation.test.ts +1143 -0
- package/src/generate-text/run-tools-transformation.ts +420 -0
- package/src/generate-text/smooth-stream.test.ts +2101 -0
- package/src/generate-text/smooth-stream.ts +162 -0
- package/src/generate-text/step-result.ts +238 -0
- package/src/generate-text/stop-condition.ts +29 -0
- package/src/generate-text/stream-text-result.ts +463 -0
- package/src/generate-text/stream-text.test-d.ts +200 -0
- package/src/generate-text/stream-text.test.ts +19979 -0
- package/src/generate-text/stream-text.ts +2505 -0
- package/src/generate-text/to-response-messages.test.ts +922 -0
- package/src/generate-text/to-response-messages.ts +163 -0
- package/src/generate-text/tool-approval-request-output.ts +21 -0
- package/src/generate-text/tool-call-repair-function.ts +27 -0
- package/src/generate-text/tool-call.ts +47 -0
- package/src/generate-text/tool-error.ts +34 -0
- package/src/generate-text/tool-output-denied.ts +21 -0
- package/src/generate-text/tool-output.ts +7 -0
- package/src/generate-text/tool-result.ts +36 -0
- package/src/generate-text/tool-set.ts +14 -0
- package/src/global.ts +24 -0
- package/src/index.ts +50 -0
- package/src/logger/index.ts +6 -0
- package/src/logger/log-warnings.test.ts +351 -0
- package/src/logger/log-warnings.ts +119 -0
- package/src/middleware/__snapshots__/simulate-streaming-middleware.test.ts.snap +64 -0
- package/src/middleware/add-tool-input-examples-middleware.test.ts +476 -0
- package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
- package/src/middleware/default-embedding-settings-middleware.test.ts +126 -0
- package/src/middleware/default-embedding-settings-middleware.ts +22 -0
- package/src/middleware/default-settings-middleware.test.ts +388 -0
- package/src/middleware/default-settings-middleware.ts +33 -0
- package/src/middleware/extract-json-middleware.test.ts +827 -0
- package/src/middleware/extract-json-middleware.ts +197 -0
- package/src/middleware/extract-reasoning-middleware.test.ts +1028 -0
- package/src/middleware/extract-reasoning-middleware.ts +238 -0
- package/src/middleware/index.ts +10 -0
- package/src/middleware/simulate-streaming-middleware.test.ts +911 -0
- package/src/middleware/simulate-streaming-middleware.ts +79 -0
- package/src/middleware/wrap-embedding-model.test.ts +358 -0
- package/src/middleware/wrap-embedding-model.ts +86 -0
- package/src/middleware/wrap-image-model.test.ts +423 -0
- package/src/middleware/wrap-image-model.ts +85 -0
- package/src/middleware/wrap-language-model.test.ts +518 -0
- package/src/middleware/wrap-language-model.ts +104 -0
- package/src/middleware/wrap-provider.test.ts +120 -0
- package/src/middleware/wrap-provider.ts +51 -0
- package/src/model/as-embedding-model-v3.test.ts +319 -0
- package/src/model/as-embedding-model-v3.ts +24 -0
- package/src/model/as-image-model-v3.test.ts +409 -0
- package/src/model/as-image-model-v3.ts +24 -0
- package/src/model/as-language-model-v3.test.ts +508 -0
- package/src/model/as-language-model-v3.ts +103 -0
- package/src/model/as-provider-v3.ts +36 -0
- package/src/model/as-speech-model-v3.test.ts +356 -0
- package/src/model/as-speech-model-v3.ts +24 -0
- package/src/model/as-transcription-model-v3.test.ts +529 -0
- package/src/model/as-transcription-model-v3.ts +24 -0
- package/src/model/resolve-model.test.ts +244 -0
- package/src/model/resolve-model.ts +126 -0
- package/src/prompt/call-settings.ts +148 -0
- package/src/prompt/content-part.ts +209 -0
- package/src/prompt/convert-to-language-model-prompt.test.ts +2018 -0
- package/src/prompt/convert-to-language-model-prompt.ts +442 -0
- package/src/prompt/create-tool-model-output.test.ts +508 -0
- package/src/prompt/create-tool-model-output.ts +34 -0
- package/src/prompt/data-content.test.ts +15 -0
- package/src/prompt/data-content.ts +134 -0
- package/src/prompt/index.ts +27 -0
- package/src/prompt/invalid-data-content-error.ts +29 -0
- package/src/prompt/invalid-message-role-error.ts +27 -0
- package/src/prompt/message-conversion-error.ts +28 -0
- package/src/prompt/message.ts +68 -0
- package/src/prompt/prepare-call-settings.test.ts +159 -0
- package/src/prompt/prepare-call-settings.ts +108 -0
- package/src/prompt/prepare-tools-and-tool-choice.test.ts +461 -0
- package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
- package/src/prompt/prompt.ts +43 -0
- package/src/prompt/split-data-url.ts +17 -0
- package/src/prompt/standardize-prompt.test.ts +82 -0
- package/src/prompt/standardize-prompt.ts +99 -0
- package/src/prompt/wrap-gateway-error.ts +29 -0
- package/src/registry/custom-provider.test.ts +211 -0
- package/src/registry/custom-provider.ts +155 -0
- package/src/registry/index.ts +7 -0
- package/src/registry/no-such-provider-error.ts +41 -0
- package/src/registry/provider-registry.test.ts +691 -0
- package/src/registry/provider-registry.ts +328 -0
- package/src/rerank/index.ts +2 -0
- package/src/rerank/rerank-result.ts +70 -0
- package/src/rerank/rerank.test.ts +516 -0
- package/src/rerank/rerank.ts +237 -0
- package/src/telemetry/assemble-operation-name.ts +21 -0
- package/src/telemetry/get-base-telemetry-attributes.ts +53 -0
- package/src/telemetry/get-tracer.ts +20 -0
- package/src/telemetry/noop-tracer.ts +69 -0
- package/src/telemetry/record-span.ts +63 -0
- package/src/telemetry/select-telemetry-attributes.ts +78 -0
- package/src/telemetry/select-temetry-attributes.test.ts +114 -0
- package/src/telemetry/stringify-for-telemetry.test.ts +114 -0
- package/src/telemetry/stringify-for-telemetry.ts +33 -0
- package/src/telemetry/telemetry-settings.ts +44 -0
- package/src/test/mock-embedding-model-v2.ts +35 -0
- package/src/test/mock-embedding-model-v3.ts +48 -0
- package/src/test/mock-image-model-v2.ts +28 -0
- package/src/test/mock-image-model-v3.ts +28 -0
- package/src/test/mock-language-model-v2.ts +72 -0
- package/src/test/mock-language-model-v3.ts +77 -0
- package/src/test/mock-provider-v2.ts +68 -0
- package/src/test/mock-provider-v3.ts +80 -0
- package/src/test/mock-reranking-model-v3.ts +25 -0
- package/src/test/mock-server-response.ts +69 -0
- package/src/test/mock-speech-model-v2.ts +24 -0
- package/src/test/mock-speech-model-v3.ts +24 -0
- package/src/test/mock-tracer.ts +156 -0
- package/src/test/mock-transcription-model-v2.ts +24 -0
- package/src/test/mock-transcription-model-v3.ts +24 -0
- package/src/test/mock-values.ts +4 -0
- package/src/test/not-implemented.ts +3 -0
- package/src/text-stream/create-text-stream-response.test.ts +38 -0
- package/src/text-stream/create-text-stream-response.ts +18 -0
- package/src/text-stream/index.ts +2 -0
- package/src/text-stream/pipe-text-stream-to-response.test.ts +38 -0
- package/src/text-stream/pipe-text-stream-to-response.ts +26 -0
- package/src/transcribe/index.ts +2 -0
- package/src/transcribe/transcribe-result.ts +60 -0
- package/src/transcribe/transcribe.test.ts +313 -0
- package/src/transcribe/transcribe.ts +173 -0
- package/src/types/embedding-model-middleware.ts +3 -0
- package/src/types/embedding-model.ts +18 -0
- package/src/types/image-model-middleware.ts +3 -0
- package/src/types/image-model-response-metadata.ts +16 -0
- package/src/types/image-model.ts +19 -0
- package/src/types/index.ts +29 -0
- package/src/types/json-value.ts +15 -0
- package/src/types/language-model-middleware.ts +3 -0
- package/src/types/language-model-request-metadata.ts +6 -0
- package/src/types/language-model-response-metadata.ts +21 -0
- package/src/types/language-model.ts +104 -0
- package/src/types/provider-metadata.ts +16 -0
- package/src/types/provider.ts +55 -0
- package/src/types/reranking-model.ts +6 -0
- package/src/types/speech-model-response-metadata.ts +21 -0
- package/src/types/speech-model.ts +6 -0
- package/src/types/transcription-model-response-metadata.ts +16 -0
- package/src/types/transcription-model.ts +9 -0
- package/src/types/usage.ts +200 -0
- package/src/types/warning.ts +7 -0
- package/src/ui/__snapshots__/append-response-messages.test.ts.snap +416 -0
- package/src/ui/__snapshots__/convert-to-model-messages.test.ts.snap +419 -0
- package/src/ui/__snapshots__/process-chat-text-response.test.ts.snap +142 -0
- package/src/ui/call-completion-api.ts +157 -0
- package/src/ui/chat-transport.ts +83 -0
- package/src/ui/chat.test-d.ts +233 -0
- package/src/ui/chat.test.ts +2695 -0
- package/src/ui/chat.ts +716 -0
- package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
- package/src/ui/convert-to-model-messages.test.ts +2775 -0
- package/src/ui/convert-to-model-messages.ts +373 -0
- package/src/ui/default-chat-transport.ts +36 -0
- package/src/ui/direct-chat-transport.test.ts +446 -0
- package/src/ui/direct-chat-transport.ts +118 -0
- package/src/ui/http-chat-transport.test.ts +185 -0
- package/src/ui/http-chat-transport.ts +292 -0
- package/src/ui/index.ts +71 -0
- package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.test.ts +371 -0
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
- package/src/ui/process-text-stream.test.ts +38 -0
- package/src/ui/process-text-stream.ts +16 -0
- package/src/ui/process-ui-message-stream.test.ts +8052 -0
- package/src/ui/process-ui-message-stream.ts +713 -0
- package/src/ui/text-stream-chat-transport.ts +23 -0
- package/src/ui/transform-text-to-ui-message-stream.test.ts +124 -0
- package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
- package/src/ui/ui-messages.test.ts +48 -0
- package/src/ui/ui-messages.ts +534 -0
- package/src/ui/use-completion.ts +84 -0
- package/src/ui/validate-ui-messages.test.ts +1428 -0
- package/src/ui/validate-ui-messages.ts +476 -0
- package/src/ui-message-stream/create-ui-message-stream-response.test.ts +266 -0
- package/src/ui-message-stream/create-ui-message-stream-response.ts +32 -0
- package/src/ui-message-stream/create-ui-message-stream.test.ts +639 -0
- package/src/ui-message-stream/create-ui-message-stream.ts +124 -0
- package/src/ui-message-stream/get-response-ui-message-id.test.ts +55 -0
- package/src/ui-message-stream/get-response-ui-message-id.ts +24 -0
- package/src/ui-message-stream/handle-ui-message-stream-finish.test.ts +429 -0
- package/src/ui-message-stream/handle-ui-message-stream-finish.ts +135 -0
- package/src/ui-message-stream/index.ts +13 -0
- package/src/ui-message-stream/json-to-sse-transform-stream.ts +12 -0
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.test.ts +90 -0
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +40 -0
- package/src/ui-message-stream/read-ui-message-stream.test.ts +122 -0
- package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
- package/src/ui-message-stream/ui-message-chunks.test-d.ts +18 -0
- package/src/ui-message-stream/ui-message-chunks.ts +344 -0
- package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
- package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
- package/src/ui-message-stream/ui-message-stream-response-init.ts +5 -0
- package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
- package/src/util/as-array.ts +3 -0
- package/src/util/async-iterable-stream.test.ts +241 -0
- package/src/util/async-iterable-stream.ts +94 -0
- package/src/util/consume-stream.ts +29 -0
- package/src/util/cosine-similarity.test.ts +57 -0
- package/src/util/cosine-similarity.ts +47 -0
- package/src/util/create-resolvable-promise.ts +30 -0
- package/src/util/create-stitchable-stream.test.ts +239 -0
- package/src/util/create-stitchable-stream.ts +112 -0
- package/src/util/data-url.ts +17 -0
- package/src/util/deep-partial.ts +84 -0
- package/src/util/detect-media-type.test.ts +670 -0
- package/src/util/detect-media-type.ts +184 -0
- package/src/util/download/download-function.ts +45 -0
- package/src/util/download/download.test.ts +69 -0
- package/src/util/download/download.ts +46 -0
- package/src/util/error-handler.ts +1 -0
- package/src/util/fix-json.test.ts +279 -0
- package/src/util/fix-json.ts +401 -0
- package/src/util/get-potential-start-index.test.ts +34 -0
- package/src/util/get-potential-start-index.ts +30 -0
- package/src/util/index.ts +11 -0
- package/src/util/is-deep-equal-data.test.ts +119 -0
- package/src/util/is-deep-equal-data.ts +48 -0
- package/src/util/is-non-empty-object.ts +5 -0
- package/src/util/job.ts +1 -0
- package/src/util/log-v2-compatibility-warning.ts +21 -0
- package/src/util/merge-abort-signals.test.ts +155 -0
- package/src/util/merge-abort-signals.ts +43 -0
- package/src/util/merge-objects.test.ts +118 -0
- package/src/util/merge-objects.ts +79 -0
- package/src/util/now.ts +4 -0
- package/src/util/parse-partial-json.test.ts +80 -0
- package/src/util/parse-partial-json.ts +30 -0
- package/src/util/prepare-headers.test.ts +51 -0
- package/src/util/prepare-headers.ts +14 -0
- package/src/util/prepare-retries.test.ts +10 -0
- package/src/util/prepare-retries.ts +47 -0
- package/src/util/retry-error.ts +41 -0
- package/src/util/retry-with-exponential-backoff.test.ts +446 -0
- package/src/util/retry-with-exponential-backoff.ts +154 -0
- package/src/util/serial-job-executor.test.ts +162 -0
- package/src/util/serial-job-executor.ts +36 -0
- package/src/util/simulate-readable-stream.test.ts +98 -0
- package/src/util/simulate-readable-stream.ts +39 -0
- package/src/util/split-array.test.ts +60 -0
- package/src/util/split-array.ts +20 -0
- package/src/util/value-of.ts +65 -0
- package/src/util/write-to-server-response.test.ts +266 -0
- package/src/util/write-to-server-response.ts +49 -0
- package/src/version.ts +5 -0
|
@@ -0,0 +1,2695 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createTestServer,
|
|
3
|
+
TestResponseController,
|
|
4
|
+
} from '@ai-sdk/test-server/with-vitest';
|
|
5
|
+
import { mockId } from '@ai-sdk/provider-utils/test';
|
|
6
|
+
import { createResolvablePromise } from '../util/create-resolvable-promise';
|
|
7
|
+
import { AbstractChat, ChatInit, ChatState, ChatStatus } from './chat';
|
|
8
|
+
import { UIMessage } from './ui-messages';
|
|
9
|
+
import { UIMessageChunk } from '../ui-message-stream/ui-message-chunks';
|
|
10
|
+
import { DefaultChatTransport } from './default-chat-transport';
|
|
11
|
+
import { lastAssistantMessageIsCompleteWithToolCalls } from './last-assistant-message-is-complete-with-tool-calls';
|
|
12
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
13
|
+
import { delay } from '@ai-sdk/provider-utils';
|
|
14
|
+
import { lastAssistantMessageIsCompleteWithApprovalResponses } from './last-assistant-message-is-complete-with-approval-responses';
|
|
15
|
+
|
|
16
|
+
class TestChatState<UI_MESSAGE extends UIMessage>
|
|
17
|
+
implements ChatState<UI_MESSAGE>
|
|
18
|
+
{
|
|
19
|
+
history: UI_MESSAGE[][] = [];
|
|
20
|
+
|
|
21
|
+
status: ChatStatus = 'ready';
|
|
22
|
+
messages: UI_MESSAGE[];
|
|
23
|
+
error: Error | undefined = undefined;
|
|
24
|
+
|
|
25
|
+
constructor(initialMessages: UI_MESSAGE[] = []) {
|
|
26
|
+
this.messages = initialMessages;
|
|
27
|
+
this.history.push(structuredClone(initialMessages));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
pushMessage = (message: UI_MESSAGE) => {
|
|
31
|
+
this.messages = this.messages.concat(message);
|
|
32
|
+
this.history.push(structuredClone(this.messages));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
popMessage = () => {
|
|
36
|
+
this.messages = this.messages.slice(0, -1);
|
|
37
|
+
this.history.push(structuredClone(this.messages));
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
replaceMessage = (index: number, message: UI_MESSAGE) => {
|
|
41
|
+
this.messages = [
|
|
42
|
+
...this.messages.slice(0, index),
|
|
43
|
+
message,
|
|
44
|
+
...this.messages.slice(index + 1),
|
|
45
|
+
];
|
|
46
|
+
this.history.push(structuredClone(this.messages));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
snapshot = <T>(value: T): T => value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class TestChat extends AbstractChat<UIMessage> {
|
|
53
|
+
constructor(init: ChatInit<UIMessage>) {
|
|
54
|
+
super({
|
|
55
|
+
...init,
|
|
56
|
+
state: new TestChatState(init.messages ?? []),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get history() {
|
|
61
|
+
return (this.state as TestChatState<UIMessage>).history;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function formatChunk(part: UIMessageChunk) {
|
|
66
|
+
return `data: ${JSON.stringify(part)}\n\n`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const server = createTestServer({
|
|
70
|
+
'http://localhost:3000/api/chat': {},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('Chat', () => {
|
|
74
|
+
describe('send a simple message', () => {
|
|
75
|
+
let chat: TestChat;
|
|
76
|
+
let letOnFinishArgs: any[] = [];
|
|
77
|
+
|
|
78
|
+
beforeEach(async () => {
|
|
79
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
80
|
+
type: 'stream-chunks',
|
|
81
|
+
chunks: [
|
|
82
|
+
formatChunk({ type: 'start' }),
|
|
83
|
+
formatChunk({ type: 'start-step' }),
|
|
84
|
+
formatChunk({ type: 'text-start', id: 'text-1' }),
|
|
85
|
+
formatChunk({
|
|
86
|
+
type: 'text-delta',
|
|
87
|
+
id: 'text-1',
|
|
88
|
+
delta: 'Hello',
|
|
89
|
+
}),
|
|
90
|
+
formatChunk({ type: 'text-delta', id: 'text-1', delta: ',' }),
|
|
91
|
+
formatChunk({
|
|
92
|
+
type: 'text-delta',
|
|
93
|
+
id: 'text-1',
|
|
94
|
+
delta: ' world',
|
|
95
|
+
}),
|
|
96
|
+
formatChunk({ type: 'text-delta', id: 'text-1', delta: '.' }),
|
|
97
|
+
formatChunk({ type: 'text-end', id: 'text-1' }),
|
|
98
|
+
formatChunk({ type: 'finish-step' }),
|
|
99
|
+
formatChunk({
|
|
100
|
+
type: 'finish',
|
|
101
|
+
finishReason: 'stop',
|
|
102
|
+
}),
|
|
103
|
+
],
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const finishPromise = createResolvablePromise<void>();
|
|
107
|
+
letOnFinishArgs = [];
|
|
108
|
+
|
|
109
|
+
chat = new TestChat({
|
|
110
|
+
id: '123',
|
|
111
|
+
generateId: mockId(),
|
|
112
|
+
transport: new DefaultChatTransport({
|
|
113
|
+
api: 'http://localhost:3000/api/chat',
|
|
114
|
+
}),
|
|
115
|
+
onFinish: (...args) => {
|
|
116
|
+
letOnFinishArgs = args;
|
|
117
|
+
return finishPromise.resolve();
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
chat.sendMessage({
|
|
122
|
+
text: 'Hello, world!',
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await finishPromise.promise;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should call onFinish with message and messages', async () => {
|
|
129
|
+
expect(letOnFinishArgs).toMatchInlineSnapshot(`
|
|
130
|
+
[
|
|
131
|
+
{
|
|
132
|
+
"finishReason": "stop",
|
|
133
|
+
"isAbort": false,
|
|
134
|
+
"isDisconnect": false,
|
|
135
|
+
"isError": false,
|
|
136
|
+
"message": {
|
|
137
|
+
"id": "id-1",
|
|
138
|
+
"metadata": undefined,
|
|
139
|
+
"parts": [
|
|
140
|
+
{
|
|
141
|
+
"type": "step-start",
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"providerMetadata": undefined,
|
|
145
|
+
"state": "done",
|
|
146
|
+
"text": "Hello, world.",
|
|
147
|
+
"type": "text",
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
"role": "assistant",
|
|
151
|
+
},
|
|
152
|
+
"messages": [
|
|
153
|
+
{
|
|
154
|
+
"id": "id-0",
|
|
155
|
+
"metadata": undefined,
|
|
156
|
+
"parts": [
|
|
157
|
+
{
|
|
158
|
+
"text": "Hello, world!",
|
|
159
|
+
"type": "text",
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
"role": "user",
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"id": "id-1",
|
|
166
|
+
"metadata": undefined,
|
|
167
|
+
"parts": [
|
|
168
|
+
{
|
|
169
|
+
"type": "step-start",
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"providerMetadata": undefined,
|
|
173
|
+
"state": "done",
|
|
174
|
+
"text": "Hello, world.",
|
|
175
|
+
"type": "text",
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
"role": "assistant",
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
},
|
|
182
|
+
]
|
|
183
|
+
`);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('should send the messages to the API', async () => {
|
|
187
|
+
expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(
|
|
188
|
+
`
|
|
189
|
+
{
|
|
190
|
+
"id": "123",
|
|
191
|
+
"messages": [
|
|
192
|
+
{
|
|
193
|
+
"id": "id-0",
|
|
194
|
+
"parts": [
|
|
195
|
+
{
|
|
196
|
+
"text": "Hello, world!",
|
|
197
|
+
"type": "text",
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
"role": "user",
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
"trigger": "submit-message",
|
|
204
|
+
}
|
|
205
|
+
`,
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should return the correct final messages', async () => {
|
|
210
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
211
|
+
[
|
|
212
|
+
{
|
|
213
|
+
"id": "id-0",
|
|
214
|
+
"metadata": undefined,
|
|
215
|
+
"parts": [
|
|
216
|
+
{
|
|
217
|
+
"text": "Hello, world!",
|
|
218
|
+
"type": "text",
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
"role": "user",
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
"id": "id-1",
|
|
225
|
+
"metadata": undefined,
|
|
226
|
+
"parts": [
|
|
227
|
+
{
|
|
228
|
+
"type": "step-start",
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
"providerMetadata": undefined,
|
|
232
|
+
"state": "done",
|
|
233
|
+
"text": "Hello, world.",
|
|
234
|
+
"type": "text",
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
"role": "assistant",
|
|
238
|
+
},
|
|
239
|
+
]
|
|
240
|
+
`);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should update the messages during the streaming', async () => {
|
|
244
|
+
expect(chat.history).toMatchInlineSnapshot(`
|
|
245
|
+
[
|
|
246
|
+
[],
|
|
247
|
+
[
|
|
248
|
+
{
|
|
249
|
+
"id": "id-0",
|
|
250
|
+
"metadata": undefined,
|
|
251
|
+
"parts": [
|
|
252
|
+
{
|
|
253
|
+
"text": "Hello, world!",
|
|
254
|
+
"type": "text",
|
|
255
|
+
},
|
|
256
|
+
],
|
|
257
|
+
"role": "user",
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
{
|
|
262
|
+
"id": "id-0",
|
|
263
|
+
"metadata": undefined,
|
|
264
|
+
"parts": [
|
|
265
|
+
{
|
|
266
|
+
"text": "Hello, world!",
|
|
267
|
+
"type": "text",
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
"role": "user",
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"id": "id-1",
|
|
274
|
+
"metadata": undefined,
|
|
275
|
+
"parts": [
|
|
276
|
+
{
|
|
277
|
+
"type": "step-start",
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
"providerMetadata": undefined,
|
|
281
|
+
"state": "streaming",
|
|
282
|
+
"text": "",
|
|
283
|
+
"type": "text",
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
"role": "assistant",
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
[
|
|
290
|
+
{
|
|
291
|
+
"id": "id-0",
|
|
292
|
+
"metadata": undefined,
|
|
293
|
+
"parts": [
|
|
294
|
+
{
|
|
295
|
+
"text": "Hello, world!",
|
|
296
|
+
"type": "text",
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
"role": "user",
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
"id": "id-1",
|
|
303
|
+
"metadata": undefined,
|
|
304
|
+
"parts": [
|
|
305
|
+
{
|
|
306
|
+
"type": "step-start",
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"providerMetadata": undefined,
|
|
310
|
+
"state": "streaming",
|
|
311
|
+
"text": "Hello",
|
|
312
|
+
"type": "text",
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
"role": "assistant",
|
|
316
|
+
},
|
|
317
|
+
],
|
|
318
|
+
[
|
|
319
|
+
{
|
|
320
|
+
"id": "id-0",
|
|
321
|
+
"metadata": undefined,
|
|
322
|
+
"parts": [
|
|
323
|
+
{
|
|
324
|
+
"text": "Hello, world!",
|
|
325
|
+
"type": "text",
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
"role": "user",
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
"id": "id-1",
|
|
332
|
+
"metadata": undefined,
|
|
333
|
+
"parts": [
|
|
334
|
+
{
|
|
335
|
+
"type": "step-start",
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
"providerMetadata": undefined,
|
|
339
|
+
"state": "streaming",
|
|
340
|
+
"text": "Hello,",
|
|
341
|
+
"type": "text",
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
"role": "assistant",
|
|
345
|
+
},
|
|
346
|
+
],
|
|
347
|
+
[
|
|
348
|
+
{
|
|
349
|
+
"id": "id-0",
|
|
350
|
+
"metadata": undefined,
|
|
351
|
+
"parts": [
|
|
352
|
+
{
|
|
353
|
+
"text": "Hello, world!",
|
|
354
|
+
"type": "text",
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
"role": "user",
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
"id": "id-1",
|
|
361
|
+
"metadata": undefined,
|
|
362
|
+
"parts": [
|
|
363
|
+
{
|
|
364
|
+
"type": "step-start",
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
"providerMetadata": undefined,
|
|
368
|
+
"state": "streaming",
|
|
369
|
+
"text": "Hello, world",
|
|
370
|
+
"type": "text",
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
"role": "assistant",
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
[
|
|
377
|
+
{
|
|
378
|
+
"id": "id-0",
|
|
379
|
+
"metadata": undefined,
|
|
380
|
+
"parts": [
|
|
381
|
+
{
|
|
382
|
+
"text": "Hello, world!",
|
|
383
|
+
"type": "text",
|
|
384
|
+
},
|
|
385
|
+
],
|
|
386
|
+
"role": "user",
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
"id": "id-1",
|
|
390
|
+
"metadata": undefined,
|
|
391
|
+
"parts": [
|
|
392
|
+
{
|
|
393
|
+
"type": "step-start",
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
"providerMetadata": undefined,
|
|
397
|
+
"state": "streaming",
|
|
398
|
+
"text": "Hello, world.",
|
|
399
|
+
"type": "text",
|
|
400
|
+
},
|
|
401
|
+
],
|
|
402
|
+
"role": "assistant",
|
|
403
|
+
},
|
|
404
|
+
],
|
|
405
|
+
[
|
|
406
|
+
{
|
|
407
|
+
"id": "id-0",
|
|
408
|
+
"metadata": undefined,
|
|
409
|
+
"parts": [
|
|
410
|
+
{
|
|
411
|
+
"text": "Hello, world!",
|
|
412
|
+
"type": "text",
|
|
413
|
+
},
|
|
414
|
+
],
|
|
415
|
+
"role": "user",
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
"id": "id-1",
|
|
419
|
+
"metadata": undefined,
|
|
420
|
+
"parts": [
|
|
421
|
+
{
|
|
422
|
+
"type": "step-start",
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
"providerMetadata": undefined,
|
|
426
|
+
"state": "done",
|
|
427
|
+
"text": "Hello, world.",
|
|
428
|
+
"type": "text",
|
|
429
|
+
},
|
|
430
|
+
],
|
|
431
|
+
"role": "assistant",
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
]
|
|
435
|
+
`);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
describe('send handle a disconnected response stream', () => {
|
|
440
|
+
let chat: TestChat;
|
|
441
|
+
let letOnFinishArgs: any[] = [];
|
|
442
|
+
|
|
443
|
+
beforeEach(async () => {
|
|
444
|
+
const controller = new TestResponseController();
|
|
445
|
+
|
|
446
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
447
|
+
type: 'controlled-stream',
|
|
448
|
+
controller,
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const finishPromise = createResolvablePromise<void>();
|
|
452
|
+
letOnFinishArgs = [];
|
|
453
|
+
|
|
454
|
+
chat = new TestChat({
|
|
455
|
+
id: '123',
|
|
456
|
+
generateId: mockId(),
|
|
457
|
+
transport: new DefaultChatTransport({
|
|
458
|
+
api: 'http://localhost:3000/api/chat',
|
|
459
|
+
}),
|
|
460
|
+
onFinish: (...args) => {
|
|
461
|
+
letOnFinishArgs = args;
|
|
462
|
+
return finishPromise.resolve();
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
chat.sendMessage({
|
|
467
|
+
text: 'Hello, world!',
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
controller.write(formatChunk({ type: 'start' }));
|
|
471
|
+
controller.write(formatChunk({ type: 'start-step' }));
|
|
472
|
+
controller.write(formatChunk({ type: 'text-start', id: 'text-1' }));
|
|
473
|
+
controller.write(
|
|
474
|
+
formatChunk({ type: 'text-delta', id: 'text-1', delta: 'Hello' }),
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
// wait until the stream is consumed before sending the error
|
|
478
|
+
while ((chat.messages[1]?.parts[1] as any)?.text !== 'Hello') {
|
|
479
|
+
await delay();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
controller.error(new TypeError('fetch failed'));
|
|
483
|
+
|
|
484
|
+
await finishPromise.promise;
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('should call onFinish with message and messages', async () => {
|
|
488
|
+
expect(letOnFinishArgs).toMatchInlineSnapshot(`
|
|
489
|
+
[
|
|
490
|
+
{
|
|
491
|
+
"finishReason": undefined,
|
|
492
|
+
"isAbort": false,
|
|
493
|
+
"isDisconnect": true,
|
|
494
|
+
"isError": true,
|
|
495
|
+
"message": {
|
|
496
|
+
"id": "id-1",
|
|
497
|
+
"metadata": undefined,
|
|
498
|
+
"parts": [
|
|
499
|
+
{
|
|
500
|
+
"type": "step-start",
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
"providerMetadata": undefined,
|
|
504
|
+
"state": "streaming",
|
|
505
|
+
"text": "Hello",
|
|
506
|
+
"type": "text",
|
|
507
|
+
},
|
|
508
|
+
],
|
|
509
|
+
"role": "assistant",
|
|
510
|
+
},
|
|
511
|
+
"messages": [
|
|
512
|
+
{
|
|
513
|
+
"id": "id-0",
|
|
514
|
+
"metadata": undefined,
|
|
515
|
+
"parts": [
|
|
516
|
+
{
|
|
517
|
+
"text": "Hello, world!",
|
|
518
|
+
"type": "text",
|
|
519
|
+
},
|
|
520
|
+
],
|
|
521
|
+
"role": "user",
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
"id": "id-1",
|
|
525
|
+
"metadata": undefined,
|
|
526
|
+
"parts": [
|
|
527
|
+
{
|
|
528
|
+
"type": "step-start",
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
"providerMetadata": undefined,
|
|
532
|
+
"state": "streaming",
|
|
533
|
+
"text": "Hello",
|
|
534
|
+
"type": "text",
|
|
535
|
+
},
|
|
536
|
+
],
|
|
537
|
+
"role": "assistant",
|
|
538
|
+
},
|
|
539
|
+
],
|
|
540
|
+
},
|
|
541
|
+
]
|
|
542
|
+
`);
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
it('should return the correct final messages', async () => {
|
|
546
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
547
|
+
[
|
|
548
|
+
{
|
|
549
|
+
"id": "id-0",
|
|
550
|
+
"metadata": undefined,
|
|
551
|
+
"parts": [
|
|
552
|
+
{
|
|
553
|
+
"text": "Hello, world!",
|
|
554
|
+
"type": "text",
|
|
555
|
+
},
|
|
556
|
+
],
|
|
557
|
+
"role": "user",
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
"id": "id-1",
|
|
561
|
+
"metadata": undefined,
|
|
562
|
+
"parts": [
|
|
563
|
+
{
|
|
564
|
+
"type": "step-start",
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
"providerMetadata": undefined,
|
|
568
|
+
"state": "streaming",
|
|
569
|
+
"text": "Hello",
|
|
570
|
+
"type": "text",
|
|
571
|
+
},
|
|
572
|
+
],
|
|
573
|
+
"role": "assistant",
|
|
574
|
+
},
|
|
575
|
+
]
|
|
576
|
+
`);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it('should update the messages during the streaming', async () => {
|
|
580
|
+
expect(chat.history).toMatchInlineSnapshot(`
|
|
581
|
+
[
|
|
582
|
+
[],
|
|
583
|
+
[
|
|
584
|
+
{
|
|
585
|
+
"id": "id-0",
|
|
586
|
+
"metadata": undefined,
|
|
587
|
+
"parts": [
|
|
588
|
+
{
|
|
589
|
+
"text": "Hello, world!",
|
|
590
|
+
"type": "text",
|
|
591
|
+
},
|
|
592
|
+
],
|
|
593
|
+
"role": "user",
|
|
594
|
+
},
|
|
595
|
+
],
|
|
596
|
+
[
|
|
597
|
+
{
|
|
598
|
+
"id": "id-0",
|
|
599
|
+
"metadata": undefined,
|
|
600
|
+
"parts": [
|
|
601
|
+
{
|
|
602
|
+
"text": "Hello, world!",
|
|
603
|
+
"type": "text",
|
|
604
|
+
},
|
|
605
|
+
],
|
|
606
|
+
"role": "user",
|
|
607
|
+
},
|
|
608
|
+
{
|
|
609
|
+
"id": "id-1",
|
|
610
|
+
"metadata": undefined,
|
|
611
|
+
"parts": [
|
|
612
|
+
{
|
|
613
|
+
"type": "step-start",
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
"providerMetadata": undefined,
|
|
617
|
+
"state": "streaming",
|
|
618
|
+
"text": "",
|
|
619
|
+
"type": "text",
|
|
620
|
+
},
|
|
621
|
+
],
|
|
622
|
+
"role": "assistant",
|
|
623
|
+
},
|
|
624
|
+
],
|
|
625
|
+
[
|
|
626
|
+
{
|
|
627
|
+
"id": "id-0",
|
|
628
|
+
"metadata": undefined,
|
|
629
|
+
"parts": [
|
|
630
|
+
{
|
|
631
|
+
"text": "Hello, world!",
|
|
632
|
+
"type": "text",
|
|
633
|
+
},
|
|
634
|
+
],
|
|
635
|
+
"role": "user",
|
|
636
|
+
},
|
|
637
|
+
{
|
|
638
|
+
"id": "id-1",
|
|
639
|
+
"metadata": undefined,
|
|
640
|
+
"parts": [
|
|
641
|
+
{
|
|
642
|
+
"type": "step-start",
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
"providerMetadata": undefined,
|
|
646
|
+
"state": "streaming",
|
|
647
|
+
"text": "Hello",
|
|
648
|
+
"type": "text",
|
|
649
|
+
},
|
|
650
|
+
],
|
|
651
|
+
"role": "assistant",
|
|
652
|
+
},
|
|
653
|
+
],
|
|
654
|
+
]
|
|
655
|
+
`);
|
|
656
|
+
});
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
describe('send handle a stop and an aborted response stream', () => {
|
|
660
|
+
let chat: TestChat;
|
|
661
|
+
let letOnFinishArgs: any[] = [];
|
|
662
|
+
let isAborted = false;
|
|
663
|
+
|
|
664
|
+
beforeEach(async () => {
|
|
665
|
+
let controller: ReadableStreamDefaultController<UIMessageChunk>;
|
|
666
|
+
const responseStream = new ReadableStream<UIMessageChunk>({
|
|
667
|
+
start: controllerArg => {
|
|
668
|
+
controller = controllerArg;
|
|
669
|
+
|
|
670
|
+
controller.enqueue({ type: 'start' });
|
|
671
|
+
controller.enqueue({ type: 'start-step' });
|
|
672
|
+
controller.enqueue({ type: 'text-start', id: 'text-1' });
|
|
673
|
+
controller.enqueue({
|
|
674
|
+
type: 'text-delta',
|
|
675
|
+
id: 'text-1',
|
|
676
|
+
delta: 'Hello',
|
|
677
|
+
});
|
|
678
|
+
},
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
const finishPromise = createResolvablePromise<void>();
|
|
682
|
+
letOnFinishArgs = [];
|
|
683
|
+
|
|
684
|
+
chat = new TestChat({
|
|
685
|
+
id: '123',
|
|
686
|
+
generateId: mockId(),
|
|
687
|
+
transport: {
|
|
688
|
+
sendMessages: async options => {
|
|
689
|
+
options.abortSignal?.addEventListener('abort', () => {
|
|
690
|
+
isAborted = true;
|
|
691
|
+
controller.error(new DOMException('Aborted', 'AbortError'));
|
|
692
|
+
});
|
|
693
|
+
return responseStream;
|
|
694
|
+
},
|
|
695
|
+
reconnectToStream: () => {
|
|
696
|
+
throw new Error('not implemented');
|
|
697
|
+
},
|
|
698
|
+
},
|
|
699
|
+
onFinish: (...args) => {
|
|
700
|
+
letOnFinishArgs = args;
|
|
701
|
+
return finishPromise.resolve();
|
|
702
|
+
},
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
chat.sendMessage({
|
|
706
|
+
text: 'Hello, world!',
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
// wait until the stream is consumed before sending the error
|
|
710
|
+
while ((chat.messages[1]?.parts[1] as any)?.text !== 'Hello') {
|
|
711
|
+
await delay();
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
await chat.stop();
|
|
715
|
+
|
|
716
|
+
await finishPromise.promise;
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
it('should have been aborted', async () => {
|
|
720
|
+
expect(isAborted).toBe(true);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
it('should call onFinish with message and messages', async () => {
|
|
724
|
+
expect(letOnFinishArgs).toMatchInlineSnapshot(`
|
|
725
|
+
[
|
|
726
|
+
{
|
|
727
|
+
"finishReason": undefined,
|
|
728
|
+
"isAbort": true,
|
|
729
|
+
"isDisconnect": false,
|
|
730
|
+
"isError": false,
|
|
731
|
+
"message": {
|
|
732
|
+
"id": "id-1",
|
|
733
|
+
"metadata": undefined,
|
|
734
|
+
"parts": [
|
|
735
|
+
{
|
|
736
|
+
"type": "step-start",
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
"providerMetadata": undefined,
|
|
740
|
+
"state": "streaming",
|
|
741
|
+
"text": "Hello",
|
|
742
|
+
"type": "text",
|
|
743
|
+
},
|
|
744
|
+
],
|
|
745
|
+
"role": "assistant",
|
|
746
|
+
},
|
|
747
|
+
"messages": [
|
|
748
|
+
{
|
|
749
|
+
"id": "id-0",
|
|
750
|
+
"metadata": undefined,
|
|
751
|
+
"parts": [
|
|
752
|
+
{
|
|
753
|
+
"text": "Hello, world!",
|
|
754
|
+
"type": "text",
|
|
755
|
+
},
|
|
756
|
+
],
|
|
757
|
+
"role": "user",
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
"id": "id-1",
|
|
761
|
+
"metadata": undefined,
|
|
762
|
+
"parts": [
|
|
763
|
+
{
|
|
764
|
+
"type": "step-start",
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
"providerMetadata": undefined,
|
|
768
|
+
"state": "streaming",
|
|
769
|
+
"text": "Hello",
|
|
770
|
+
"type": "text",
|
|
771
|
+
},
|
|
772
|
+
],
|
|
773
|
+
"role": "assistant",
|
|
774
|
+
},
|
|
775
|
+
],
|
|
776
|
+
},
|
|
777
|
+
]
|
|
778
|
+
`);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
it('should return the correct final messages', async () => {
|
|
782
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
783
|
+
[
|
|
784
|
+
{
|
|
785
|
+
"id": "id-0",
|
|
786
|
+
"metadata": undefined,
|
|
787
|
+
"parts": [
|
|
788
|
+
{
|
|
789
|
+
"text": "Hello, world!",
|
|
790
|
+
"type": "text",
|
|
791
|
+
},
|
|
792
|
+
],
|
|
793
|
+
"role": "user",
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
"id": "id-1",
|
|
797
|
+
"metadata": undefined,
|
|
798
|
+
"parts": [
|
|
799
|
+
{
|
|
800
|
+
"type": "step-start",
|
|
801
|
+
},
|
|
802
|
+
{
|
|
803
|
+
"providerMetadata": undefined,
|
|
804
|
+
"state": "streaming",
|
|
805
|
+
"text": "Hello",
|
|
806
|
+
"type": "text",
|
|
807
|
+
},
|
|
808
|
+
],
|
|
809
|
+
"role": "assistant",
|
|
810
|
+
},
|
|
811
|
+
]
|
|
812
|
+
`);
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
it('should update the messages during the streaming', async () => {
|
|
816
|
+
expect(chat.history).toMatchInlineSnapshot(`
|
|
817
|
+
[
|
|
818
|
+
[],
|
|
819
|
+
[
|
|
820
|
+
{
|
|
821
|
+
"id": "id-0",
|
|
822
|
+
"metadata": undefined,
|
|
823
|
+
"parts": [
|
|
824
|
+
{
|
|
825
|
+
"text": "Hello, world!",
|
|
826
|
+
"type": "text",
|
|
827
|
+
},
|
|
828
|
+
],
|
|
829
|
+
"role": "user",
|
|
830
|
+
},
|
|
831
|
+
],
|
|
832
|
+
[
|
|
833
|
+
{
|
|
834
|
+
"id": "id-0",
|
|
835
|
+
"metadata": undefined,
|
|
836
|
+
"parts": [
|
|
837
|
+
{
|
|
838
|
+
"text": "Hello, world!",
|
|
839
|
+
"type": "text",
|
|
840
|
+
},
|
|
841
|
+
],
|
|
842
|
+
"role": "user",
|
|
843
|
+
},
|
|
844
|
+
{
|
|
845
|
+
"id": "id-1",
|
|
846
|
+
"metadata": undefined,
|
|
847
|
+
"parts": [
|
|
848
|
+
{
|
|
849
|
+
"type": "step-start",
|
|
850
|
+
},
|
|
851
|
+
{
|
|
852
|
+
"providerMetadata": undefined,
|
|
853
|
+
"state": "streaming",
|
|
854
|
+
"text": "",
|
|
855
|
+
"type": "text",
|
|
856
|
+
},
|
|
857
|
+
],
|
|
858
|
+
"role": "assistant",
|
|
859
|
+
},
|
|
860
|
+
],
|
|
861
|
+
[
|
|
862
|
+
{
|
|
863
|
+
"id": "id-0",
|
|
864
|
+
"metadata": undefined,
|
|
865
|
+
"parts": [
|
|
866
|
+
{
|
|
867
|
+
"text": "Hello, world!",
|
|
868
|
+
"type": "text",
|
|
869
|
+
},
|
|
870
|
+
],
|
|
871
|
+
"role": "user",
|
|
872
|
+
},
|
|
873
|
+
{
|
|
874
|
+
"id": "id-1",
|
|
875
|
+
"metadata": undefined,
|
|
876
|
+
"parts": [
|
|
877
|
+
{
|
|
878
|
+
"type": "step-start",
|
|
879
|
+
},
|
|
880
|
+
{
|
|
881
|
+
"providerMetadata": undefined,
|
|
882
|
+
"state": "streaming",
|
|
883
|
+
"text": "Hello",
|
|
884
|
+
"type": "text",
|
|
885
|
+
},
|
|
886
|
+
],
|
|
887
|
+
"role": "assistant",
|
|
888
|
+
},
|
|
889
|
+
],
|
|
890
|
+
]
|
|
891
|
+
`);
|
|
892
|
+
});
|
|
893
|
+
});
|
|
894
|
+
|
|
895
|
+
it('should include the metadata of text message', async () => {
|
|
896
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
897
|
+
type: 'stream-chunks',
|
|
898
|
+
chunks: [
|
|
899
|
+
formatChunk({ type: 'start' }),
|
|
900
|
+
formatChunk({ type: 'start-step' }),
|
|
901
|
+
formatChunk({ type: 'text-start', id: 'text-1' }),
|
|
902
|
+
formatChunk({
|
|
903
|
+
type: 'text-delta',
|
|
904
|
+
id: 'text-1',
|
|
905
|
+
delta: 'Hello, world.',
|
|
906
|
+
}),
|
|
907
|
+
formatChunk({ type: 'text-end', id: 'text-1' }),
|
|
908
|
+
formatChunk({ type: 'finish-step' }),
|
|
909
|
+
formatChunk({
|
|
910
|
+
type: 'finish',
|
|
911
|
+
finishReason: 'stop',
|
|
912
|
+
}),
|
|
913
|
+
],
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
const finishPromise = createResolvablePromise<void>();
|
|
917
|
+
|
|
918
|
+
const chat = new TestChat({
|
|
919
|
+
id: '123',
|
|
920
|
+
generateId: mockId(),
|
|
921
|
+
transport: new DefaultChatTransport({
|
|
922
|
+
api: 'http://localhost:3000/api/chat',
|
|
923
|
+
}),
|
|
924
|
+
onFinish: () => finishPromise.resolve(),
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
chat.sendMessage({
|
|
928
|
+
text: 'Hello, world!',
|
|
929
|
+
metadata: { someData: true },
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
await finishPromise.promise;
|
|
933
|
+
|
|
934
|
+
expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(
|
|
935
|
+
`
|
|
936
|
+
{
|
|
937
|
+
"id": "123",
|
|
938
|
+
"messages": [
|
|
939
|
+
{
|
|
940
|
+
"id": "id-0",
|
|
941
|
+
"metadata": {
|
|
942
|
+
"someData": true,
|
|
943
|
+
},
|
|
944
|
+
"parts": [
|
|
945
|
+
{
|
|
946
|
+
"text": "Hello, world!",
|
|
947
|
+
"type": "text",
|
|
948
|
+
},
|
|
949
|
+
],
|
|
950
|
+
"role": "user",
|
|
951
|
+
},
|
|
952
|
+
],
|
|
953
|
+
"trigger": "submit-message",
|
|
954
|
+
}
|
|
955
|
+
`,
|
|
956
|
+
);
|
|
957
|
+
|
|
958
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
959
|
+
[
|
|
960
|
+
{
|
|
961
|
+
"id": "id-0",
|
|
962
|
+
"metadata": {
|
|
963
|
+
"someData": true,
|
|
964
|
+
},
|
|
965
|
+
"parts": [
|
|
966
|
+
{
|
|
967
|
+
"text": "Hello, world!",
|
|
968
|
+
"type": "text",
|
|
969
|
+
},
|
|
970
|
+
],
|
|
971
|
+
"role": "user",
|
|
972
|
+
},
|
|
973
|
+
{
|
|
974
|
+
"id": "id-1",
|
|
975
|
+
"metadata": undefined,
|
|
976
|
+
"parts": [
|
|
977
|
+
{
|
|
978
|
+
"type": "step-start",
|
|
979
|
+
},
|
|
980
|
+
{
|
|
981
|
+
"providerMetadata": undefined,
|
|
982
|
+
"state": "done",
|
|
983
|
+
"text": "Hello, world.",
|
|
984
|
+
"type": "text",
|
|
985
|
+
},
|
|
986
|
+
],
|
|
987
|
+
"role": "assistant",
|
|
988
|
+
},
|
|
989
|
+
]
|
|
990
|
+
`);
|
|
991
|
+
|
|
992
|
+
expect(chat.history).toMatchInlineSnapshot(`
|
|
993
|
+
[
|
|
994
|
+
[],
|
|
995
|
+
[
|
|
996
|
+
{
|
|
997
|
+
"id": "id-0",
|
|
998
|
+
"metadata": {
|
|
999
|
+
"someData": true,
|
|
1000
|
+
},
|
|
1001
|
+
"parts": [
|
|
1002
|
+
{
|
|
1003
|
+
"text": "Hello, world!",
|
|
1004
|
+
"type": "text",
|
|
1005
|
+
},
|
|
1006
|
+
],
|
|
1007
|
+
"role": "user",
|
|
1008
|
+
},
|
|
1009
|
+
],
|
|
1010
|
+
[
|
|
1011
|
+
{
|
|
1012
|
+
"id": "id-0",
|
|
1013
|
+
"metadata": {
|
|
1014
|
+
"someData": true,
|
|
1015
|
+
},
|
|
1016
|
+
"parts": [
|
|
1017
|
+
{
|
|
1018
|
+
"text": "Hello, world!",
|
|
1019
|
+
"type": "text",
|
|
1020
|
+
},
|
|
1021
|
+
],
|
|
1022
|
+
"role": "user",
|
|
1023
|
+
},
|
|
1024
|
+
{
|
|
1025
|
+
"id": "id-1",
|
|
1026
|
+
"metadata": undefined,
|
|
1027
|
+
"parts": [
|
|
1028
|
+
{
|
|
1029
|
+
"type": "step-start",
|
|
1030
|
+
},
|
|
1031
|
+
{
|
|
1032
|
+
"providerMetadata": undefined,
|
|
1033
|
+
"state": "streaming",
|
|
1034
|
+
"text": "",
|
|
1035
|
+
"type": "text",
|
|
1036
|
+
},
|
|
1037
|
+
],
|
|
1038
|
+
"role": "assistant",
|
|
1039
|
+
},
|
|
1040
|
+
],
|
|
1041
|
+
[
|
|
1042
|
+
{
|
|
1043
|
+
"id": "id-0",
|
|
1044
|
+
"metadata": {
|
|
1045
|
+
"someData": true,
|
|
1046
|
+
},
|
|
1047
|
+
"parts": [
|
|
1048
|
+
{
|
|
1049
|
+
"text": "Hello, world!",
|
|
1050
|
+
"type": "text",
|
|
1051
|
+
},
|
|
1052
|
+
],
|
|
1053
|
+
"role": "user",
|
|
1054
|
+
},
|
|
1055
|
+
{
|
|
1056
|
+
"id": "id-1",
|
|
1057
|
+
"metadata": undefined,
|
|
1058
|
+
"parts": [
|
|
1059
|
+
{
|
|
1060
|
+
"type": "step-start",
|
|
1061
|
+
},
|
|
1062
|
+
{
|
|
1063
|
+
"providerMetadata": undefined,
|
|
1064
|
+
"state": "streaming",
|
|
1065
|
+
"text": "Hello, world.",
|
|
1066
|
+
"type": "text",
|
|
1067
|
+
},
|
|
1068
|
+
],
|
|
1069
|
+
"role": "assistant",
|
|
1070
|
+
},
|
|
1071
|
+
],
|
|
1072
|
+
[
|
|
1073
|
+
{
|
|
1074
|
+
"id": "id-0",
|
|
1075
|
+
"metadata": {
|
|
1076
|
+
"someData": true,
|
|
1077
|
+
},
|
|
1078
|
+
"parts": [
|
|
1079
|
+
{
|
|
1080
|
+
"text": "Hello, world!",
|
|
1081
|
+
"type": "text",
|
|
1082
|
+
},
|
|
1083
|
+
],
|
|
1084
|
+
"role": "user",
|
|
1085
|
+
},
|
|
1086
|
+
{
|
|
1087
|
+
"id": "id-1",
|
|
1088
|
+
"metadata": undefined,
|
|
1089
|
+
"parts": [
|
|
1090
|
+
{
|
|
1091
|
+
"type": "step-start",
|
|
1092
|
+
},
|
|
1093
|
+
{
|
|
1094
|
+
"providerMetadata": undefined,
|
|
1095
|
+
"state": "done",
|
|
1096
|
+
"text": "Hello, world.",
|
|
1097
|
+
"type": "text",
|
|
1098
|
+
},
|
|
1099
|
+
],
|
|
1100
|
+
"role": "assistant",
|
|
1101
|
+
},
|
|
1102
|
+
],
|
|
1103
|
+
]
|
|
1104
|
+
`);
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
it('should replace an existing user message', async () => {
|
|
1108
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
1109
|
+
type: 'stream-chunks',
|
|
1110
|
+
chunks: [
|
|
1111
|
+
formatChunk({ type: 'start' }),
|
|
1112
|
+
formatChunk({ type: 'start-step' }),
|
|
1113
|
+
formatChunk({ type: 'text-start', id: 'text-1' }),
|
|
1114
|
+
formatChunk({
|
|
1115
|
+
type: 'text-delta',
|
|
1116
|
+
id: 'text-1',
|
|
1117
|
+
delta: 'Hello',
|
|
1118
|
+
}),
|
|
1119
|
+
formatChunk({ type: 'text-delta', id: 'text-1', delta: ',' }),
|
|
1120
|
+
formatChunk({
|
|
1121
|
+
type: 'text-delta',
|
|
1122
|
+
id: 'text-1',
|
|
1123
|
+
delta: ' world',
|
|
1124
|
+
}),
|
|
1125
|
+
formatChunk({ type: 'text-delta', id: 'text-1', delta: '.' }),
|
|
1126
|
+
formatChunk({ type: 'text-end', id: 'text-1' }),
|
|
1127
|
+
formatChunk({ type: 'finish-step' }),
|
|
1128
|
+
formatChunk({ type: 'finish' }),
|
|
1129
|
+
],
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
const finishPromise = createResolvablePromise<void>();
|
|
1133
|
+
|
|
1134
|
+
const chat = new TestChat({
|
|
1135
|
+
id: '123',
|
|
1136
|
+
generateId: mockId({ prefix: 'newid' }),
|
|
1137
|
+
transport: new DefaultChatTransport({
|
|
1138
|
+
api: 'http://localhost:3000/api/chat',
|
|
1139
|
+
}),
|
|
1140
|
+
onFinish: () => finishPromise.resolve(),
|
|
1141
|
+
messages: [
|
|
1142
|
+
{
|
|
1143
|
+
id: 'id-0',
|
|
1144
|
+
role: 'user',
|
|
1145
|
+
parts: [{ text: 'Hi!', type: 'text' }],
|
|
1146
|
+
},
|
|
1147
|
+
{
|
|
1148
|
+
id: 'id-1',
|
|
1149
|
+
role: 'assistant',
|
|
1150
|
+
parts: [{ text: 'How can I help you?', type: 'text', state: 'done' }],
|
|
1151
|
+
},
|
|
1152
|
+
],
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
chat.sendMessage({
|
|
1156
|
+
text: 'Hello, world!',
|
|
1157
|
+
messageId: 'id-0',
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1160
|
+
await finishPromise.promise;
|
|
1161
|
+
|
|
1162
|
+
expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(
|
|
1163
|
+
`
|
|
1164
|
+
{
|
|
1165
|
+
"id": "123",
|
|
1166
|
+
"messageId": "id-0",
|
|
1167
|
+
"messages": [
|
|
1168
|
+
{
|
|
1169
|
+
"id": "id-0",
|
|
1170
|
+
"parts": [
|
|
1171
|
+
{
|
|
1172
|
+
"text": "Hello, world!",
|
|
1173
|
+
"type": "text",
|
|
1174
|
+
},
|
|
1175
|
+
],
|
|
1176
|
+
"role": "user",
|
|
1177
|
+
},
|
|
1178
|
+
],
|
|
1179
|
+
"trigger": "submit-message",
|
|
1180
|
+
}
|
|
1181
|
+
`,
|
|
1182
|
+
);
|
|
1183
|
+
|
|
1184
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
1185
|
+
[
|
|
1186
|
+
{
|
|
1187
|
+
"id": "id-0",
|
|
1188
|
+
"metadata": undefined,
|
|
1189
|
+
"parts": [
|
|
1190
|
+
{
|
|
1191
|
+
"text": "Hello, world!",
|
|
1192
|
+
"type": "text",
|
|
1193
|
+
},
|
|
1194
|
+
],
|
|
1195
|
+
"role": "user",
|
|
1196
|
+
},
|
|
1197
|
+
{
|
|
1198
|
+
"id": "newid-0",
|
|
1199
|
+
"metadata": undefined,
|
|
1200
|
+
"parts": [
|
|
1201
|
+
{
|
|
1202
|
+
"type": "step-start",
|
|
1203
|
+
},
|
|
1204
|
+
{
|
|
1205
|
+
"providerMetadata": undefined,
|
|
1206
|
+
"state": "done",
|
|
1207
|
+
"text": "Hello, world.",
|
|
1208
|
+
"type": "text",
|
|
1209
|
+
},
|
|
1210
|
+
],
|
|
1211
|
+
"role": "assistant",
|
|
1212
|
+
},
|
|
1213
|
+
]
|
|
1214
|
+
`);
|
|
1215
|
+
|
|
1216
|
+
expect(chat.history).toMatchInlineSnapshot(`
|
|
1217
|
+
[
|
|
1218
|
+
[
|
|
1219
|
+
{
|
|
1220
|
+
"id": "id-0",
|
|
1221
|
+
"parts": [
|
|
1222
|
+
{
|
|
1223
|
+
"text": "Hi!",
|
|
1224
|
+
"type": "text",
|
|
1225
|
+
},
|
|
1226
|
+
],
|
|
1227
|
+
"role": "user",
|
|
1228
|
+
},
|
|
1229
|
+
{
|
|
1230
|
+
"id": "id-1",
|
|
1231
|
+
"parts": [
|
|
1232
|
+
{
|
|
1233
|
+
"state": "done",
|
|
1234
|
+
"text": "How can I help you?",
|
|
1235
|
+
"type": "text",
|
|
1236
|
+
},
|
|
1237
|
+
],
|
|
1238
|
+
"role": "assistant",
|
|
1239
|
+
},
|
|
1240
|
+
],
|
|
1241
|
+
[
|
|
1242
|
+
{
|
|
1243
|
+
"id": "id-0",
|
|
1244
|
+
"metadata": undefined,
|
|
1245
|
+
"parts": [
|
|
1246
|
+
{
|
|
1247
|
+
"text": "Hello, world!",
|
|
1248
|
+
"type": "text",
|
|
1249
|
+
},
|
|
1250
|
+
],
|
|
1251
|
+
"role": "user",
|
|
1252
|
+
},
|
|
1253
|
+
],
|
|
1254
|
+
[
|
|
1255
|
+
{
|
|
1256
|
+
"id": "id-0",
|
|
1257
|
+
"metadata": undefined,
|
|
1258
|
+
"parts": [
|
|
1259
|
+
{
|
|
1260
|
+
"text": "Hello, world!",
|
|
1261
|
+
"type": "text",
|
|
1262
|
+
},
|
|
1263
|
+
],
|
|
1264
|
+
"role": "user",
|
|
1265
|
+
},
|
|
1266
|
+
{
|
|
1267
|
+
"id": "newid-0",
|
|
1268
|
+
"metadata": undefined,
|
|
1269
|
+
"parts": [
|
|
1270
|
+
{
|
|
1271
|
+
"type": "step-start",
|
|
1272
|
+
},
|
|
1273
|
+
{
|
|
1274
|
+
"providerMetadata": undefined,
|
|
1275
|
+
"state": "streaming",
|
|
1276
|
+
"text": "",
|
|
1277
|
+
"type": "text",
|
|
1278
|
+
},
|
|
1279
|
+
],
|
|
1280
|
+
"role": "assistant",
|
|
1281
|
+
},
|
|
1282
|
+
],
|
|
1283
|
+
[
|
|
1284
|
+
{
|
|
1285
|
+
"id": "id-0",
|
|
1286
|
+
"metadata": undefined,
|
|
1287
|
+
"parts": [
|
|
1288
|
+
{
|
|
1289
|
+
"text": "Hello, world!",
|
|
1290
|
+
"type": "text",
|
|
1291
|
+
},
|
|
1292
|
+
],
|
|
1293
|
+
"role": "user",
|
|
1294
|
+
},
|
|
1295
|
+
{
|
|
1296
|
+
"id": "newid-0",
|
|
1297
|
+
"metadata": undefined,
|
|
1298
|
+
"parts": [
|
|
1299
|
+
{
|
|
1300
|
+
"type": "step-start",
|
|
1301
|
+
},
|
|
1302
|
+
{
|
|
1303
|
+
"providerMetadata": undefined,
|
|
1304
|
+
"state": "streaming",
|
|
1305
|
+
"text": "Hello",
|
|
1306
|
+
"type": "text",
|
|
1307
|
+
},
|
|
1308
|
+
],
|
|
1309
|
+
"role": "assistant",
|
|
1310
|
+
},
|
|
1311
|
+
],
|
|
1312
|
+
[
|
|
1313
|
+
{
|
|
1314
|
+
"id": "id-0",
|
|
1315
|
+
"metadata": undefined,
|
|
1316
|
+
"parts": [
|
|
1317
|
+
{
|
|
1318
|
+
"text": "Hello, world!",
|
|
1319
|
+
"type": "text",
|
|
1320
|
+
},
|
|
1321
|
+
],
|
|
1322
|
+
"role": "user",
|
|
1323
|
+
},
|
|
1324
|
+
{
|
|
1325
|
+
"id": "newid-0",
|
|
1326
|
+
"metadata": undefined,
|
|
1327
|
+
"parts": [
|
|
1328
|
+
{
|
|
1329
|
+
"type": "step-start",
|
|
1330
|
+
},
|
|
1331
|
+
{
|
|
1332
|
+
"providerMetadata": undefined,
|
|
1333
|
+
"state": "streaming",
|
|
1334
|
+
"text": "Hello,",
|
|
1335
|
+
"type": "text",
|
|
1336
|
+
},
|
|
1337
|
+
],
|
|
1338
|
+
"role": "assistant",
|
|
1339
|
+
},
|
|
1340
|
+
],
|
|
1341
|
+
[
|
|
1342
|
+
{
|
|
1343
|
+
"id": "id-0",
|
|
1344
|
+
"metadata": undefined,
|
|
1345
|
+
"parts": [
|
|
1346
|
+
{
|
|
1347
|
+
"text": "Hello, world!",
|
|
1348
|
+
"type": "text",
|
|
1349
|
+
},
|
|
1350
|
+
],
|
|
1351
|
+
"role": "user",
|
|
1352
|
+
},
|
|
1353
|
+
{
|
|
1354
|
+
"id": "newid-0",
|
|
1355
|
+
"metadata": undefined,
|
|
1356
|
+
"parts": [
|
|
1357
|
+
{
|
|
1358
|
+
"type": "step-start",
|
|
1359
|
+
},
|
|
1360
|
+
{
|
|
1361
|
+
"providerMetadata": undefined,
|
|
1362
|
+
"state": "streaming",
|
|
1363
|
+
"text": "Hello, world",
|
|
1364
|
+
"type": "text",
|
|
1365
|
+
},
|
|
1366
|
+
],
|
|
1367
|
+
"role": "assistant",
|
|
1368
|
+
},
|
|
1369
|
+
],
|
|
1370
|
+
[
|
|
1371
|
+
{
|
|
1372
|
+
"id": "id-0",
|
|
1373
|
+
"metadata": undefined,
|
|
1374
|
+
"parts": [
|
|
1375
|
+
{
|
|
1376
|
+
"text": "Hello, world!",
|
|
1377
|
+
"type": "text",
|
|
1378
|
+
},
|
|
1379
|
+
],
|
|
1380
|
+
"role": "user",
|
|
1381
|
+
},
|
|
1382
|
+
{
|
|
1383
|
+
"id": "newid-0",
|
|
1384
|
+
"metadata": undefined,
|
|
1385
|
+
"parts": [
|
|
1386
|
+
{
|
|
1387
|
+
"type": "step-start",
|
|
1388
|
+
},
|
|
1389
|
+
{
|
|
1390
|
+
"providerMetadata": undefined,
|
|
1391
|
+
"state": "streaming",
|
|
1392
|
+
"text": "Hello, world.",
|
|
1393
|
+
"type": "text",
|
|
1394
|
+
},
|
|
1395
|
+
],
|
|
1396
|
+
"role": "assistant",
|
|
1397
|
+
},
|
|
1398
|
+
],
|
|
1399
|
+
[
|
|
1400
|
+
{
|
|
1401
|
+
"id": "id-0",
|
|
1402
|
+
"metadata": undefined,
|
|
1403
|
+
"parts": [
|
|
1404
|
+
{
|
|
1405
|
+
"text": "Hello, world!",
|
|
1406
|
+
"type": "text",
|
|
1407
|
+
},
|
|
1408
|
+
],
|
|
1409
|
+
"role": "user",
|
|
1410
|
+
},
|
|
1411
|
+
{
|
|
1412
|
+
"id": "newid-0",
|
|
1413
|
+
"metadata": undefined,
|
|
1414
|
+
"parts": [
|
|
1415
|
+
{
|
|
1416
|
+
"type": "step-start",
|
|
1417
|
+
},
|
|
1418
|
+
{
|
|
1419
|
+
"providerMetadata": undefined,
|
|
1420
|
+
"state": "done",
|
|
1421
|
+
"text": "Hello, world.",
|
|
1422
|
+
"type": "text",
|
|
1423
|
+
},
|
|
1424
|
+
],
|
|
1425
|
+
"role": "assistant",
|
|
1426
|
+
},
|
|
1427
|
+
],
|
|
1428
|
+
]
|
|
1429
|
+
`);
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
it('should handle error parts', async () => {
|
|
1433
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
1434
|
+
type: 'stream-chunks',
|
|
1435
|
+
chunks: [
|
|
1436
|
+
formatChunk({ type: 'start' }),
|
|
1437
|
+
formatChunk({ type: 'error', errorText: 'test-error' }),
|
|
1438
|
+
],
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
const errorPromise = createResolvablePromise<void>();
|
|
1442
|
+
|
|
1443
|
+
const chat = new TestChat({
|
|
1444
|
+
id: '123',
|
|
1445
|
+
generateId: mockId(),
|
|
1446
|
+
transport: new DefaultChatTransport({
|
|
1447
|
+
api: 'http://localhost:3000/api/chat',
|
|
1448
|
+
}),
|
|
1449
|
+
onError: () => errorPromise.resolve(),
|
|
1450
|
+
});
|
|
1451
|
+
|
|
1452
|
+
chat.sendMessage({
|
|
1453
|
+
text: 'Hello, world!',
|
|
1454
|
+
});
|
|
1455
|
+
|
|
1456
|
+
await errorPromise.promise;
|
|
1457
|
+
|
|
1458
|
+
expect(chat.error).toMatchInlineSnapshot(`[Error: test-error]`);
|
|
1459
|
+
expect(chat.status).toBe('error');
|
|
1460
|
+
});
|
|
1461
|
+
|
|
1462
|
+
describe('sendAutomaticallyWhen', () => {
|
|
1463
|
+
it('should delay tool output submission until the stream is finished', async () => {
|
|
1464
|
+
const controller1 = new TestResponseController();
|
|
1465
|
+
|
|
1466
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
1467
|
+
{ type: 'controlled-stream', controller: controller1 },
|
|
1468
|
+
{ type: 'stream-chunks', chunks: [formatChunk({ type: 'start' })] },
|
|
1469
|
+
];
|
|
1470
|
+
|
|
1471
|
+
const toolCallPromise = createResolvablePromise<void>();
|
|
1472
|
+
const submitMessagePromise = createResolvablePromise<void>();
|
|
1473
|
+
let callCount = 0;
|
|
1474
|
+
|
|
1475
|
+
const chat = new TestChat({
|
|
1476
|
+
id: '123',
|
|
1477
|
+
generateId: mockId(),
|
|
1478
|
+
transport: new DefaultChatTransport({
|
|
1479
|
+
api: 'http://localhost:3000/api/chat',
|
|
1480
|
+
}),
|
|
1481
|
+
sendAutomaticallyWhen: () => callCount < 2,
|
|
1482
|
+
onToolCall: () => toolCallPromise.resolve(),
|
|
1483
|
+
onFinish: () => {
|
|
1484
|
+
callCount++;
|
|
1485
|
+
},
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
chat
|
|
1489
|
+
.sendMessage({
|
|
1490
|
+
text: 'Hello, world!',
|
|
1491
|
+
})
|
|
1492
|
+
.then(() => {
|
|
1493
|
+
submitMessagePromise.resolve();
|
|
1494
|
+
});
|
|
1495
|
+
|
|
1496
|
+
// start stream
|
|
1497
|
+
controller1.write(formatChunk({ type: 'start' }));
|
|
1498
|
+
controller1.write(formatChunk({ type: 'start-step' }));
|
|
1499
|
+
|
|
1500
|
+
// tool call
|
|
1501
|
+
controller1.write(
|
|
1502
|
+
formatChunk({
|
|
1503
|
+
type: 'tool-input-available',
|
|
1504
|
+
toolCallId: 'tool-call-0',
|
|
1505
|
+
toolName: 'test-tool',
|
|
1506
|
+
input: { testArg: 'test-value' },
|
|
1507
|
+
}),
|
|
1508
|
+
);
|
|
1509
|
+
|
|
1510
|
+
await toolCallPromise.promise;
|
|
1511
|
+
|
|
1512
|
+
// user submits the tool output
|
|
1513
|
+
await chat.addToolOutput({
|
|
1514
|
+
tool: 'test-tool',
|
|
1515
|
+
toolCallId: 'tool-call-0',
|
|
1516
|
+
output: 'test-output',
|
|
1517
|
+
});
|
|
1518
|
+
|
|
1519
|
+
// UI should show the tool output
|
|
1520
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
1521
|
+
[
|
|
1522
|
+
{
|
|
1523
|
+
"id": "id-0",
|
|
1524
|
+
"metadata": undefined,
|
|
1525
|
+
"parts": [
|
|
1526
|
+
{
|
|
1527
|
+
"text": "Hello, world!",
|
|
1528
|
+
"type": "text",
|
|
1529
|
+
},
|
|
1530
|
+
],
|
|
1531
|
+
"role": "user",
|
|
1532
|
+
},
|
|
1533
|
+
{
|
|
1534
|
+
"id": "id-1",
|
|
1535
|
+
"metadata": undefined,
|
|
1536
|
+
"parts": [
|
|
1537
|
+
{
|
|
1538
|
+
"type": "step-start",
|
|
1539
|
+
},
|
|
1540
|
+
{
|
|
1541
|
+
"errorText": undefined,
|
|
1542
|
+
"input": {
|
|
1543
|
+
"testArg": "test-value",
|
|
1544
|
+
},
|
|
1545
|
+
"output": "test-output",
|
|
1546
|
+
"preliminary": undefined,
|
|
1547
|
+
"providerExecuted": undefined,
|
|
1548
|
+
"rawInput": undefined,
|
|
1549
|
+
"state": "output-available",
|
|
1550
|
+
"title": undefined,
|
|
1551
|
+
"toolCallId": "tool-call-0",
|
|
1552
|
+
"type": "tool-test-tool",
|
|
1553
|
+
},
|
|
1554
|
+
],
|
|
1555
|
+
"role": "assistant",
|
|
1556
|
+
},
|
|
1557
|
+
]
|
|
1558
|
+
`);
|
|
1559
|
+
|
|
1560
|
+
// should not have called the API yet
|
|
1561
|
+
expect(server.calls.length).toBe(1);
|
|
1562
|
+
|
|
1563
|
+
// finish stream
|
|
1564
|
+
controller1.write(formatChunk({ type: 'finish-step' }));
|
|
1565
|
+
controller1.write(
|
|
1566
|
+
formatChunk({
|
|
1567
|
+
type: 'finish',
|
|
1568
|
+
finishReason: 'stop',
|
|
1569
|
+
}),
|
|
1570
|
+
);
|
|
1571
|
+
|
|
1572
|
+
await controller1.close();
|
|
1573
|
+
|
|
1574
|
+
await submitMessagePromise.promise;
|
|
1575
|
+
|
|
1576
|
+
// 2nd call should happen after the stream is finished
|
|
1577
|
+
expect(server.calls.length).toBe(2);
|
|
1578
|
+
|
|
1579
|
+
// check details of the 2nd call
|
|
1580
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
1581
|
+
{
|
|
1582
|
+
"id": "123",
|
|
1583
|
+
"messageId": "id-1",
|
|
1584
|
+
"messages": [
|
|
1585
|
+
{
|
|
1586
|
+
"id": "id-0",
|
|
1587
|
+
"parts": [
|
|
1588
|
+
{
|
|
1589
|
+
"text": "Hello, world!",
|
|
1590
|
+
"type": "text",
|
|
1591
|
+
},
|
|
1592
|
+
],
|
|
1593
|
+
"role": "user",
|
|
1594
|
+
},
|
|
1595
|
+
{
|
|
1596
|
+
"id": "id-1",
|
|
1597
|
+
"parts": [
|
|
1598
|
+
{
|
|
1599
|
+
"type": "step-start",
|
|
1600
|
+
},
|
|
1601
|
+
{
|
|
1602
|
+
"input": {
|
|
1603
|
+
"testArg": "test-value",
|
|
1604
|
+
},
|
|
1605
|
+
"output": "test-output",
|
|
1606
|
+
"state": "output-available",
|
|
1607
|
+
"toolCallId": "tool-call-0",
|
|
1608
|
+
"type": "tool-test-tool",
|
|
1609
|
+
},
|
|
1610
|
+
],
|
|
1611
|
+
"role": "assistant",
|
|
1612
|
+
},
|
|
1613
|
+
],
|
|
1614
|
+
"trigger": "submit-message",
|
|
1615
|
+
}
|
|
1616
|
+
`);
|
|
1617
|
+
});
|
|
1618
|
+
|
|
1619
|
+
it('should send message when a tool output is submitted', async () => {
|
|
1620
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
1621
|
+
{
|
|
1622
|
+
type: 'stream-chunks',
|
|
1623
|
+
chunks: [
|
|
1624
|
+
formatChunk({ type: 'start' }),
|
|
1625
|
+
formatChunk({ type: 'start-step' }),
|
|
1626
|
+
formatChunk({
|
|
1627
|
+
type: 'tool-input-available',
|
|
1628
|
+
toolCallId: 'tool-call-0',
|
|
1629
|
+
toolName: 'test-tool',
|
|
1630
|
+
input: { testArg: 'test-value' },
|
|
1631
|
+
}),
|
|
1632
|
+
formatChunk({ type: 'finish-step' }),
|
|
1633
|
+
formatChunk({ type: 'finish' }),
|
|
1634
|
+
],
|
|
1635
|
+
},
|
|
1636
|
+
{
|
|
1637
|
+
type: 'stream-chunks',
|
|
1638
|
+
chunks: [
|
|
1639
|
+
formatChunk({ type: 'start' }),
|
|
1640
|
+
formatChunk({ type: 'start-step' }),
|
|
1641
|
+
formatChunk({ type: 'finish-step' }),
|
|
1642
|
+
formatChunk({ type: 'finish' }),
|
|
1643
|
+
],
|
|
1644
|
+
},
|
|
1645
|
+
];
|
|
1646
|
+
|
|
1647
|
+
let callCount = 0;
|
|
1648
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
1649
|
+
|
|
1650
|
+
const chat = new TestChat({
|
|
1651
|
+
id: '123',
|
|
1652
|
+
generateId: mockId(),
|
|
1653
|
+
transport: new DefaultChatTransport({
|
|
1654
|
+
api: 'http://localhost:3000/api/chat',
|
|
1655
|
+
}),
|
|
1656
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
1657
|
+
onFinish: () => {
|
|
1658
|
+
callCount++;
|
|
1659
|
+
if (callCount === 2) {
|
|
1660
|
+
onFinishPromise.resolve();
|
|
1661
|
+
}
|
|
1662
|
+
},
|
|
1663
|
+
});
|
|
1664
|
+
|
|
1665
|
+
await chat.sendMessage({
|
|
1666
|
+
text: 'Hello, world!',
|
|
1667
|
+
});
|
|
1668
|
+
|
|
1669
|
+
// user submits the tool output
|
|
1670
|
+
await chat.addToolOutput({
|
|
1671
|
+
tool: 'test-tool',
|
|
1672
|
+
toolCallId: 'tool-call-0',
|
|
1673
|
+
output: 'test-output',
|
|
1674
|
+
});
|
|
1675
|
+
|
|
1676
|
+
// UI should show the tool output
|
|
1677
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
1678
|
+
[
|
|
1679
|
+
{
|
|
1680
|
+
"id": "id-0",
|
|
1681
|
+
"metadata": undefined,
|
|
1682
|
+
"parts": [
|
|
1683
|
+
{
|
|
1684
|
+
"text": "Hello, world!",
|
|
1685
|
+
"type": "text",
|
|
1686
|
+
},
|
|
1687
|
+
],
|
|
1688
|
+
"role": "user",
|
|
1689
|
+
},
|
|
1690
|
+
{
|
|
1691
|
+
"id": "id-1",
|
|
1692
|
+
"metadata": undefined,
|
|
1693
|
+
"parts": [
|
|
1694
|
+
{
|
|
1695
|
+
"type": "step-start",
|
|
1696
|
+
},
|
|
1697
|
+
{
|
|
1698
|
+
"errorText": undefined,
|
|
1699
|
+
"input": {
|
|
1700
|
+
"testArg": "test-value",
|
|
1701
|
+
},
|
|
1702
|
+
"output": "test-output",
|
|
1703
|
+
"preliminary": undefined,
|
|
1704
|
+
"providerExecuted": undefined,
|
|
1705
|
+
"rawInput": undefined,
|
|
1706
|
+
"state": "output-available",
|
|
1707
|
+
"title": undefined,
|
|
1708
|
+
"toolCallId": "tool-call-0",
|
|
1709
|
+
"type": "tool-test-tool",
|
|
1710
|
+
},
|
|
1711
|
+
],
|
|
1712
|
+
"role": "assistant",
|
|
1713
|
+
},
|
|
1714
|
+
]
|
|
1715
|
+
`);
|
|
1716
|
+
|
|
1717
|
+
await onFinishPromise.promise;
|
|
1718
|
+
|
|
1719
|
+
// 2nd call should happen after the stream is finished
|
|
1720
|
+
expect(server.calls.length).toBe(2);
|
|
1721
|
+
|
|
1722
|
+
// check details of the 2nd call
|
|
1723
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
1724
|
+
{
|
|
1725
|
+
"id": "123",
|
|
1726
|
+
"messageId": "id-1",
|
|
1727
|
+
"messages": [
|
|
1728
|
+
{
|
|
1729
|
+
"id": "id-0",
|
|
1730
|
+
"parts": [
|
|
1731
|
+
{
|
|
1732
|
+
"text": "Hello, world!",
|
|
1733
|
+
"type": "text",
|
|
1734
|
+
},
|
|
1735
|
+
],
|
|
1736
|
+
"role": "user",
|
|
1737
|
+
},
|
|
1738
|
+
{
|
|
1739
|
+
"id": "id-1",
|
|
1740
|
+
"parts": [
|
|
1741
|
+
{
|
|
1742
|
+
"type": "step-start",
|
|
1743
|
+
},
|
|
1744
|
+
{
|
|
1745
|
+
"input": {
|
|
1746
|
+
"testArg": "test-value",
|
|
1747
|
+
},
|
|
1748
|
+
"output": "test-output",
|
|
1749
|
+
"state": "output-available",
|
|
1750
|
+
"toolCallId": "tool-call-0",
|
|
1751
|
+
"type": "tool-test-tool",
|
|
1752
|
+
},
|
|
1753
|
+
],
|
|
1754
|
+
"role": "assistant",
|
|
1755
|
+
},
|
|
1756
|
+
],
|
|
1757
|
+
"trigger": "submit-message",
|
|
1758
|
+
}
|
|
1759
|
+
`);
|
|
1760
|
+
});
|
|
1761
|
+
|
|
1762
|
+
it('should send message when a tool error result is submitted', async () => {
|
|
1763
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
1764
|
+
{
|
|
1765
|
+
type: 'stream-chunks',
|
|
1766
|
+
chunks: [
|
|
1767
|
+
formatChunk({ type: 'start' }),
|
|
1768
|
+
formatChunk({ type: 'start-step' }),
|
|
1769
|
+
formatChunk({
|
|
1770
|
+
type: 'tool-input-available',
|
|
1771
|
+
toolCallId: 'tool-call-0',
|
|
1772
|
+
toolName: 'test-tool',
|
|
1773
|
+
input: { testArg: 'test-value' },
|
|
1774
|
+
}),
|
|
1775
|
+
formatChunk({ type: 'finish-step' }),
|
|
1776
|
+
formatChunk({ type: 'finish' }),
|
|
1777
|
+
],
|
|
1778
|
+
},
|
|
1779
|
+
{
|
|
1780
|
+
type: 'stream-chunks',
|
|
1781
|
+
chunks: [
|
|
1782
|
+
formatChunk({ type: 'start' }),
|
|
1783
|
+
formatChunk({ type: 'start-step' }),
|
|
1784
|
+
formatChunk({ type: 'finish-step' }),
|
|
1785
|
+
formatChunk({ type: 'finish' }),
|
|
1786
|
+
],
|
|
1787
|
+
},
|
|
1788
|
+
];
|
|
1789
|
+
|
|
1790
|
+
let callCount = 0;
|
|
1791
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
1792
|
+
|
|
1793
|
+
const chat = new TestChat({
|
|
1794
|
+
id: '123',
|
|
1795
|
+
generateId: mockId(),
|
|
1796
|
+
transport: new DefaultChatTransport({
|
|
1797
|
+
api: 'http://localhost:3000/api/chat',
|
|
1798
|
+
}),
|
|
1799
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
1800
|
+
onFinish: () => {
|
|
1801
|
+
callCount++;
|
|
1802
|
+
if (callCount === 2) {
|
|
1803
|
+
onFinishPromise.resolve();
|
|
1804
|
+
}
|
|
1805
|
+
},
|
|
1806
|
+
});
|
|
1807
|
+
|
|
1808
|
+
await chat.sendMessage({
|
|
1809
|
+
text: 'Hello, world!',
|
|
1810
|
+
});
|
|
1811
|
+
|
|
1812
|
+
// user submits the tool output
|
|
1813
|
+
await chat.addToolOutput({
|
|
1814
|
+
state: 'output-error',
|
|
1815
|
+
tool: 'test-tool',
|
|
1816
|
+
toolCallId: 'tool-call-0',
|
|
1817
|
+
errorText: 'test-error',
|
|
1818
|
+
});
|
|
1819
|
+
|
|
1820
|
+
// UI should show the tool output
|
|
1821
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
1822
|
+
[
|
|
1823
|
+
{
|
|
1824
|
+
"id": "id-0",
|
|
1825
|
+
"metadata": undefined,
|
|
1826
|
+
"parts": [
|
|
1827
|
+
{
|
|
1828
|
+
"text": "Hello, world!",
|
|
1829
|
+
"type": "text",
|
|
1830
|
+
},
|
|
1831
|
+
],
|
|
1832
|
+
"role": "user",
|
|
1833
|
+
},
|
|
1834
|
+
{
|
|
1835
|
+
"id": "id-1",
|
|
1836
|
+
"metadata": undefined,
|
|
1837
|
+
"parts": [
|
|
1838
|
+
{
|
|
1839
|
+
"type": "step-start",
|
|
1840
|
+
},
|
|
1841
|
+
{
|
|
1842
|
+
"errorText": "test-error",
|
|
1843
|
+
"input": {
|
|
1844
|
+
"testArg": "test-value",
|
|
1845
|
+
},
|
|
1846
|
+
"output": undefined,
|
|
1847
|
+
"preliminary": undefined,
|
|
1848
|
+
"providerExecuted": undefined,
|
|
1849
|
+
"rawInput": undefined,
|
|
1850
|
+
"state": "output-error",
|
|
1851
|
+
"title": undefined,
|
|
1852
|
+
"toolCallId": "tool-call-0",
|
|
1853
|
+
"type": "tool-test-tool",
|
|
1854
|
+
},
|
|
1855
|
+
],
|
|
1856
|
+
"role": "assistant",
|
|
1857
|
+
},
|
|
1858
|
+
]
|
|
1859
|
+
`);
|
|
1860
|
+
|
|
1861
|
+
await onFinishPromise.promise;
|
|
1862
|
+
|
|
1863
|
+
// 2nd call should happen after the stream is finished
|
|
1864
|
+
expect(server.calls.length).toBe(2);
|
|
1865
|
+
|
|
1866
|
+
// check details of the 2nd call
|
|
1867
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
1868
|
+
{
|
|
1869
|
+
"id": "123",
|
|
1870
|
+
"messageId": "id-1",
|
|
1871
|
+
"messages": [
|
|
1872
|
+
{
|
|
1873
|
+
"id": "id-0",
|
|
1874
|
+
"parts": [
|
|
1875
|
+
{
|
|
1876
|
+
"text": "Hello, world!",
|
|
1877
|
+
"type": "text",
|
|
1878
|
+
},
|
|
1879
|
+
],
|
|
1880
|
+
"role": "user",
|
|
1881
|
+
},
|
|
1882
|
+
{
|
|
1883
|
+
"id": "id-1",
|
|
1884
|
+
"parts": [
|
|
1885
|
+
{
|
|
1886
|
+
"type": "step-start",
|
|
1887
|
+
},
|
|
1888
|
+
{
|
|
1889
|
+
"errorText": "test-error",
|
|
1890
|
+
"input": {
|
|
1891
|
+
"testArg": "test-value",
|
|
1892
|
+
},
|
|
1893
|
+
"state": "output-error",
|
|
1894
|
+
"toolCallId": "tool-call-0",
|
|
1895
|
+
"type": "tool-test-tool",
|
|
1896
|
+
},
|
|
1897
|
+
],
|
|
1898
|
+
"role": "assistant",
|
|
1899
|
+
},
|
|
1900
|
+
],
|
|
1901
|
+
"trigger": "submit-message",
|
|
1902
|
+
}
|
|
1903
|
+
`);
|
|
1904
|
+
});
|
|
1905
|
+
|
|
1906
|
+
it('should send message when a dynamic tool output is submitted', async () => {
|
|
1907
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
1908
|
+
{
|
|
1909
|
+
type: 'stream-chunks',
|
|
1910
|
+
chunks: [
|
|
1911
|
+
formatChunk({ type: 'start' }),
|
|
1912
|
+
formatChunk({ type: 'start-step' }),
|
|
1913
|
+
formatChunk({
|
|
1914
|
+
type: 'tool-input-available',
|
|
1915
|
+
dynamic: true,
|
|
1916
|
+
toolCallId: 'tool-call-0',
|
|
1917
|
+
toolName: 'test-tool',
|
|
1918
|
+
input: { testArg: 'test-value' },
|
|
1919
|
+
}),
|
|
1920
|
+
formatChunk({ type: 'finish-step' }),
|
|
1921
|
+
formatChunk({ type: 'finish' }),
|
|
1922
|
+
],
|
|
1923
|
+
},
|
|
1924
|
+
{
|
|
1925
|
+
type: 'stream-chunks',
|
|
1926
|
+
chunks: [
|
|
1927
|
+
formatChunk({ type: 'start' }),
|
|
1928
|
+
formatChunk({ type: 'start-step' }),
|
|
1929
|
+
formatChunk({ type: 'text-start', id: 'id-1' }),
|
|
1930
|
+
formatChunk({
|
|
1931
|
+
type: 'text-delta',
|
|
1932
|
+
id: 'id-1',
|
|
1933
|
+
delta: 'test-delta',
|
|
1934
|
+
}),
|
|
1935
|
+
formatChunk({ type: 'text-end', id: 'id-1' }),
|
|
1936
|
+
formatChunk({ type: 'finish-step' }),
|
|
1937
|
+
formatChunk({
|
|
1938
|
+
type: 'finish',
|
|
1939
|
+
finishReason: 'stop',
|
|
1940
|
+
}),
|
|
1941
|
+
],
|
|
1942
|
+
},
|
|
1943
|
+
];
|
|
1944
|
+
|
|
1945
|
+
let callCount = 0;
|
|
1946
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
1947
|
+
|
|
1948
|
+
const chat = new TestChat({
|
|
1949
|
+
id: '123',
|
|
1950
|
+
generateId: mockId(),
|
|
1951
|
+
transport: new DefaultChatTransport({
|
|
1952
|
+
api: 'http://localhost:3000/api/chat',
|
|
1953
|
+
}),
|
|
1954
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
1955
|
+
onFinish: () => {
|
|
1956
|
+
callCount++;
|
|
1957
|
+
if (callCount === 2) {
|
|
1958
|
+
onFinishPromise.resolve();
|
|
1959
|
+
}
|
|
1960
|
+
},
|
|
1961
|
+
});
|
|
1962
|
+
|
|
1963
|
+
await chat.sendMessage({
|
|
1964
|
+
text: 'Hello, world!',
|
|
1965
|
+
});
|
|
1966
|
+
|
|
1967
|
+
// user submits the tool output
|
|
1968
|
+
await chat.addToolOutput({
|
|
1969
|
+
state: 'output-available',
|
|
1970
|
+
tool: 'test-tool',
|
|
1971
|
+
toolCallId: 'tool-call-0',
|
|
1972
|
+
output: 'test-output',
|
|
1973
|
+
});
|
|
1974
|
+
|
|
1975
|
+
// UI should show the tool output
|
|
1976
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
1977
|
+
[
|
|
1978
|
+
{
|
|
1979
|
+
"id": "id-0",
|
|
1980
|
+
"metadata": undefined,
|
|
1981
|
+
"parts": [
|
|
1982
|
+
{
|
|
1983
|
+
"text": "Hello, world!",
|
|
1984
|
+
"type": "text",
|
|
1985
|
+
},
|
|
1986
|
+
],
|
|
1987
|
+
"role": "user",
|
|
1988
|
+
},
|
|
1989
|
+
{
|
|
1990
|
+
"id": "id-1",
|
|
1991
|
+
"metadata": undefined,
|
|
1992
|
+
"parts": [
|
|
1993
|
+
{
|
|
1994
|
+
"type": "step-start",
|
|
1995
|
+
},
|
|
1996
|
+
{
|
|
1997
|
+
"errorText": undefined,
|
|
1998
|
+
"input": {
|
|
1999
|
+
"testArg": "test-value",
|
|
2000
|
+
},
|
|
2001
|
+
"output": "test-output",
|
|
2002
|
+
"preliminary": undefined,
|
|
2003
|
+
"providerExecuted": undefined,
|
|
2004
|
+
"state": "output-available",
|
|
2005
|
+
"title": undefined,
|
|
2006
|
+
"toolCallId": "tool-call-0",
|
|
2007
|
+
"toolName": "test-tool",
|
|
2008
|
+
"type": "dynamic-tool",
|
|
2009
|
+
},
|
|
2010
|
+
],
|
|
2011
|
+
"role": "assistant",
|
|
2012
|
+
},
|
|
2013
|
+
]
|
|
2014
|
+
`);
|
|
2015
|
+
|
|
2016
|
+
await onFinishPromise.promise;
|
|
2017
|
+
|
|
2018
|
+
// 2nd call should happen after the stream is finished
|
|
2019
|
+
expect(server.calls.length).toBe(2);
|
|
2020
|
+
|
|
2021
|
+
// check details of the 2nd call
|
|
2022
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
2023
|
+
{
|
|
2024
|
+
"id": "123",
|
|
2025
|
+
"messageId": "id-1",
|
|
2026
|
+
"messages": [
|
|
2027
|
+
{
|
|
2028
|
+
"id": "id-0",
|
|
2029
|
+
"parts": [
|
|
2030
|
+
{
|
|
2031
|
+
"text": "Hello, world!",
|
|
2032
|
+
"type": "text",
|
|
2033
|
+
},
|
|
2034
|
+
],
|
|
2035
|
+
"role": "user",
|
|
2036
|
+
},
|
|
2037
|
+
{
|
|
2038
|
+
"id": "id-1",
|
|
2039
|
+
"parts": [
|
|
2040
|
+
{
|
|
2041
|
+
"type": "step-start",
|
|
2042
|
+
},
|
|
2043
|
+
{
|
|
2044
|
+
"input": {
|
|
2045
|
+
"testArg": "test-value",
|
|
2046
|
+
},
|
|
2047
|
+
"output": "test-output",
|
|
2048
|
+
"state": "output-available",
|
|
2049
|
+
"toolCallId": "tool-call-0",
|
|
2050
|
+
"toolName": "test-tool",
|
|
2051
|
+
"type": "dynamic-tool",
|
|
2052
|
+
},
|
|
2053
|
+
],
|
|
2054
|
+
"role": "assistant",
|
|
2055
|
+
},
|
|
2056
|
+
],
|
|
2057
|
+
"trigger": "submit-message",
|
|
2058
|
+
}
|
|
2059
|
+
`);
|
|
2060
|
+
|
|
2061
|
+
// UI should show the response
|
|
2062
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2063
|
+
[
|
|
2064
|
+
{
|
|
2065
|
+
"id": "id-0",
|
|
2066
|
+
"metadata": undefined,
|
|
2067
|
+
"parts": [
|
|
2068
|
+
{
|
|
2069
|
+
"text": "Hello, world!",
|
|
2070
|
+
"type": "text",
|
|
2071
|
+
},
|
|
2072
|
+
],
|
|
2073
|
+
"role": "user",
|
|
2074
|
+
},
|
|
2075
|
+
{
|
|
2076
|
+
"id": "id-1",
|
|
2077
|
+
"metadata": undefined,
|
|
2078
|
+
"parts": [
|
|
2079
|
+
{
|
|
2080
|
+
"type": "step-start",
|
|
2081
|
+
},
|
|
2082
|
+
{
|
|
2083
|
+
"errorText": undefined,
|
|
2084
|
+
"input": {
|
|
2085
|
+
"testArg": "test-value",
|
|
2086
|
+
},
|
|
2087
|
+
"output": "test-output",
|
|
2088
|
+
"preliminary": undefined,
|
|
2089
|
+
"providerExecuted": undefined,
|
|
2090
|
+
"state": "output-available",
|
|
2091
|
+
"title": undefined,
|
|
2092
|
+
"toolCallId": "tool-call-0",
|
|
2093
|
+
"toolName": "test-tool",
|
|
2094
|
+
"type": "dynamic-tool",
|
|
2095
|
+
},
|
|
2096
|
+
{
|
|
2097
|
+
"type": "step-start",
|
|
2098
|
+
},
|
|
2099
|
+
{
|
|
2100
|
+
"providerMetadata": undefined,
|
|
2101
|
+
"state": "done",
|
|
2102
|
+
"text": "test-delta",
|
|
2103
|
+
"type": "text",
|
|
2104
|
+
},
|
|
2105
|
+
],
|
|
2106
|
+
"role": "assistant",
|
|
2107
|
+
},
|
|
2108
|
+
]
|
|
2109
|
+
`);
|
|
2110
|
+
});
|
|
2111
|
+
|
|
2112
|
+
it('should not send message when the server responded with an error', async () => {
|
|
2113
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
2114
|
+
{
|
|
2115
|
+
type: 'stream-chunks',
|
|
2116
|
+
chunks: [
|
|
2117
|
+
formatChunk({ type: 'start' }),
|
|
2118
|
+
formatChunk({ type: 'start-step' }),
|
|
2119
|
+
formatChunk({
|
|
2120
|
+
type: 'tool-input-available',
|
|
2121
|
+
dynamic: true,
|
|
2122
|
+
toolCallId: 'tool-call-0',
|
|
2123
|
+
toolName: 'test-tool',
|
|
2124
|
+
input: { testArg: 'test-value' },
|
|
2125
|
+
}),
|
|
2126
|
+
formatChunk({ type: 'finish-step' }),
|
|
2127
|
+
formatChunk({ type: 'finish' }),
|
|
2128
|
+
],
|
|
2129
|
+
},
|
|
2130
|
+
{
|
|
2131
|
+
type: 'error',
|
|
2132
|
+
status: 500,
|
|
2133
|
+
body: 'Internal Server Error',
|
|
2134
|
+
},
|
|
2135
|
+
];
|
|
2136
|
+
|
|
2137
|
+
let callCount = 0;
|
|
2138
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
2139
|
+
|
|
2140
|
+
const chat = new TestChat({
|
|
2141
|
+
id: '123',
|
|
2142
|
+
generateId: mockId(),
|
|
2143
|
+
transport: new DefaultChatTransport({
|
|
2144
|
+
api: 'http://localhost:3000/api/chat',
|
|
2145
|
+
}),
|
|
2146
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
2147
|
+
onFinish: () => {
|
|
2148
|
+
callCount++;
|
|
2149
|
+
if (callCount === 2) {
|
|
2150
|
+
onFinishPromise.resolve();
|
|
2151
|
+
}
|
|
2152
|
+
},
|
|
2153
|
+
});
|
|
2154
|
+
|
|
2155
|
+
await chat.sendMessage({
|
|
2156
|
+
text: 'Hello, world!',
|
|
2157
|
+
});
|
|
2158
|
+
|
|
2159
|
+
// user submits the tool output
|
|
2160
|
+
await chat.addToolOutput({
|
|
2161
|
+
state: 'output-available',
|
|
2162
|
+
tool: 'test-tool',
|
|
2163
|
+
toolCallId: 'tool-call-0',
|
|
2164
|
+
output: 'test-output',
|
|
2165
|
+
});
|
|
2166
|
+
|
|
2167
|
+
// UI should show the tool output
|
|
2168
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2169
|
+
[
|
|
2170
|
+
{
|
|
2171
|
+
"id": "id-0",
|
|
2172
|
+
"metadata": undefined,
|
|
2173
|
+
"parts": [
|
|
2174
|
+
{
|
|
2175
|
+
"text": "Hello, world!",
|
|
2176
|
+
"type": "text",
|
|
2177
|
+
},
|
|
2178
|
+
],
|
|
2179
|
+
"role": "user",
|
|
2180
|
+
},
|
|
2181
|
+
{
|
|
2182
|
+
"id": "id-1",
|
|
2183
|
+
"metadata": undefined,
|
|
2184
|
+
"parts": [
|
|
2185
|
+
{
|
|
2186
|
+
"type": "step-start",
|
|
2187
|
+
},
|
|
2188
|
+
{
|
|
2189
|
+
"errorText": undefined,
|
|
2190
|
+
"input": {
|
|
2191
|
+
"testArg": "test-value",
|
|
2192
|
+
},
|
|
2193
|
+
"output": "test-output",
|
|
2194
|
+
"preliminary": undefined,
|
|
2195
|
+
"providerExecuted": undefined,
|
|
2196
|
+
"state": "output-available",
|
|
2197
|
+
"title": undefined,
|
|
2198
|
+
"toolCallId": "tool-call-0",
|
|
2199
|
+
"toolName": "test-tool",
|
|
2200
|
+
"type": "dynamic-tool",
|
|
2201
|
+
},
|
|
2202
|
+
],
|
|
2203
|
+
"role": "assistant",
|
|
2204
|
+
},
|
|
2205
|
+
]
|
|
2206
|
+
`);
|
|
2207
|
+
|
|
2208
|
+
await onFinishPromise.promise;
|
|
2209
|
+
|
|
2210
|
+
// 2nd call should happen after the stream is finished
|
|
2211
|
+
expect(server.calls.length).toBe(2);
|
|
2212
|
+
|
|
2213
|
+
// check details of the 2nd call
|
|
2214
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
2215
|
+
{
|
|
2216
|
+
"id": "123",
|
|
2217
|
+
"messageId": "id-1",
|
|
2218
|
+
"messages": [
|
|
2219
|
+
{
|
|
2220
|
+
"id": "id-0",
|
|
2221
|
+
"parts": [
|
|
2222
|
+
{
|
|
2223
|
+
"text": "Hello, world!",
|
|
2224
|
+
"type": "text",
|
|
2225
|
+
},
|
|
2226
|
+
],
|
|
2227
|
+
"role": "user",
|
|
2228
|
+
},
|
|
2229
|
+
{
|
|
2230
|
+
"id": "id-1",
|
|
2231
|
+
"parts": [
|
|
2232
|
+
{
|
|
2233
|
+
"type": "step-start",
|
|
2234
|
+
},
|
|
2235
|
+
{
|
|
2236
|
+
"input": {
|
|
2237
|
+
"testArg": "test-value",
|
|
2238
|
+
},
|
|
2239
|
+
"output": "test-output",
|
|
2240
|
+
"state": "output-available",
|
|
2241
|
+
"toolCallId": "tool-call-0",
|
|
2242
|
+
"toolName": "test-tool",
|
|
2243
|
+
"type": "dynamic-tool",
|
|
2244
|
+
},
|
|
2245
|
+
],
|
|
2246
|
+
"role": "assistant",
|
|
2247
|
+
},
|
|
2248
|
+
],
|
|
2249
|
+
"trigger": "submit-message",
|
|
2250
|
+
}
|
|
2251
|
+
`);
|
|
2252
|
+
|
|
2253
|
+
// UI should not show the response
|
|
2254
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2255
|
+
[
|
|
2256
|
+
{
|
|
2257
|
+
"id": "id-0",
|
|
2258
|
+
"metadata": undefined,
|
|
2259
|
+
"parts": [
|
|
2260
|
+
{
|
|
2261
|
+
"text": "Hello, world!",
|
|
2262
|
+
"type": "text",
|
|
2263
|
+
},
|
|
2264
|
+
],
|
|
2265
|
+
"role": "user",
|
|
2266
|
+
},
|
|
2267
|
+
{
|
|
2268
|
+
"id": "id-1",
|
|
2269
|
+
"metadata": undefined,
|
|
2270
|
+
"parts": [
|
|
2271
|
+
{
|
|
2272
|
+
"type": "step-start",
|
|
2273
|
+
},
|
|
2274
|
+
{
|
|
2275
|
+
"errorText": undefined,
|
|
2276
|
+
"input": {
|
|
2277
|
+
"testArg": "test-value",
|
|
2278
|
+
},
|
|
2279
|
+
"output": "test-output",
|
|
2280
|
+
"preliminary": undefined,
|
|
2281
|
+
"providerExecuted": undefined,
|
|
2282
|
+
"state": "output-available",
|
|
2283
|
+
"title": undefined,
|
|
2284
|
+
"toolCallId": "tool-call-0",
|
|
2285
|
+
"toolName": "test-tool",
|
|
2286
|
+
"type": "dynamic-tool",
|
|
2287
|
+
},
|
|
2288
|
+
],
|
|
2289
|
+
"role": "assistant",
|
|
2290
|
+
},
|
|
2291
|
+
]
|
|
2292
|
+
`);
|
|
2293
|
+
|
|
2294
|
+
// UI should be in error state
|
|
2295
|
+
expect(chat.status).toBe('error');
|
|
2296
|
+
expect(chat.error).toMatchInlineSnapshot(
|
|
2297
|
+
`[Error: Internal Server Error]`,
|
|
2298
|
+
);
|
|
2299
|
+
});
|
|
2300
|
+
});
|
|
2301
|
+
|
|
2302
|
+
describe('clearError', () => {
|
|
2303
|
+
it('should clear the error and set the status to ready', async () => {
|
|
2304
|
+
server.urls['http://localhost:3000/api/chat'].response = {
|
|
2305
|
+
type: 'stream-chunks',
|
|
2306
|
+
chunks: [
|
|
2307
|
+
formatChunk({ type: 'start' }),
|
|
2308
|
+
formatChunk({ type: 'error', errorText: 'test-error' }),
|
|
2309
|
+
],
|
|
2310
|
+
};
|
|
2311
|
+
|
|
2312
|
+
const errorPromise = createResolvablePromise<void>();
|
|
2313
|
+
|
|
2314
|
+
const chat = new TestChat({
|
|
2315
|
+
id: '123',
|
|
2316
|
+
generateId: mockId(),
|
|
2317
|
+
transport: new DefaultChatTransport({
|
|
2318
|
+
api: 'http://localhost:3000/api/chat',
|
|
2319
|
+
}),
|
|
2320
|
+
onError: () => errorPromise.resolve(),
|
|
2321
|
+
});
|
|
2322
|
+
|
|
2323
|
+
chat.sendMessage({
|
|
2324
|
+
text: 'Hello, world!',
|
|
2325
|
+
});
|
|
2326
|
+
|
|
2327
|
+
await errorPromise.promise;
|
|
2328
|
+
|
|
2329
|
+
expect(chat.error).toMatchInlineSnapshot(`[Error: test-error]`);
|
|
2330
|
+
expect(chat.status).toBe('error');
|
|
2331
|
+
|
|
2332
|
+
chat.clearError();
|
|
2333
|
+
|
|
2334
|
+
expect(chat.error).toBeUndefined();
|
|
2335
|
+
expect(chat.status).toBe('ready');
|
|
2336
|
+
});
|
|
2337
|
+
});
|
|
2338
|
+
|
|
2339
|
+
describe('addToolApprovalResponse', () => {
|
|
2340
|
+
describe('approved', () => {
|
|
2341
|
+
let chat: TestChat;
|
|
2342
|
+
|
|
2343
|
+
beforeEach(async () => {
|
|
2344
|
+
chat = new TestChat({
|
|
2345
|
+
id: '123',
|
|
2346
|
+
generateId: mockId({ prefix: 'newid' }),
|
|
2347
|
+
transport: new DefaultChatTransport({
|
|
2348
|
+
api: 'http://localhost:3000/api/chat',
|
|
2349
|
+
}),
|
|
2350
|
+
messages: [
|
|
2351
|
+
{
|
|
2352
|
+
id: 'id-0',
|
|
2353
|
+
role: 'user',
|
|
2354
|
+
parts: [{ text: 'What is the weather in Tokyo?', type: 'text' }],
|
|
2355
|
+
},
|
|
2356
|
+
{
|
|
2357
|
+
id: 'id-1',
|
|
2358
|
+
role: 'assistant',
|
|
2359
|
+
parts: [
|
|
2360
|
+
{ type: 'step-start' },
|
|
2361
|
+
{
|
|
2362
|
+
type: 'tool-weather',
|
|
2363
|
+
toolCallId: 'call-1',
|
|
2364
|
+
state: 'approval-requested',
|
|
2365
|
+
input: { city: 'Tokyo' },
|
|
2366
|
+
approval: { id: 'approval-1' },
|
|
2367
|
+
},
|
|
2368
|
+
],
|
|
2369
|
+
},
|
|
2370
|
+
],
|
|
2371
|
+
});
|
|
2372
|
+
|
|
2373
|
+
await chat.addToolApprovalResponse({
|
|
2374
|
+
id: 'approval-1',
|
|
2375
|
+
approved: true,
|
|
2376
|
+
});
|
|
2377
|
+
});
|
|
2378
|
+
|
|
2379
|
+
it('should update tool invocation to show the approval response', () => {
|
|
2380
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2381
|
+
[
|
|
2382
|
+
{
|
|
2383
|
+
"id": "id-0",
|
|
2384
|
+
"parts": [
|
|
2385
|
+
{
|
|
2386
|
+
"text": "What is the weather in Tokyo?",
|
|
2387
|
+
"type": "text",
|
|
2388
|
+
},
|
|
2389
|
+
],
|
|
2390
|
+
"role": "user",
|
|
2391
|
+
},
|
|
2392
|
+
{
|
|
2393
|
+
"id": "id-1",
|
|
2394
|
+
"parts": [
|
|
2395
|
+
{
|
|
2396
|
+
"type": "step-start",
|
|
2397
|
+
},
|
|
2398
|
+
{
|
|
2399
|
+
"approval": {
|
|
2400
|
+
"approved": true,
|
|
2401
|
+
"id": "approval-1",
|
|
2402
|
+
"reason": undefined,
|
|
2403
|
+
},
|
|
2404
|
+
"input": {
|
|
2405
|
+
"city": "Tokyo",
|
|
2406
|
+
},
|
|
2407
|
+
"state": "approval-responded",
|
|
2408
|
+
"toolCallId": "call-1",
|
|
2409
|
+
"type": "tool-weather",
|
|
2410
|
+
},
|
|
2411
|
+
],
|
|
2412
|
+
"role": "assistant",
|
|
2413
|
+
},
|
|
2414
|
+
]
|
|
2415
|
+
`);
|
|
2416
|
+
});
|
|
2417
|
+
});
|
|
2418
|
+
|
|
2419
|
+
describe('approved with automatic sending', () => {
|
|
2420
|
+
let chat: TestChat;
|
|
2421
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
2422
|
+
|
|
2423
|
+
beforeEach(async () => {
|
|
2424
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
2425
|
+
{
|
|
2426
|
+
type: 'stream-chunks',
|
|
2427
|
+
chunks: [
|
|
2428
|
+
formatChunk({ type: 'start' }),
|
|
2429
|
+
formatChunk({ type: 'start-step' }),
|
|
2430
|
+
formatChunk({
|
|
2431
|
+
type: 'tool-output-available',
|
|
2432
|
+
toolCallId: 'call-1',
|
|
2433
|
+
output: { temperature: 72, weather: 'sunny' },
|
|
2434
|
+
}),
|
|
2435
|
+
formatChunk({ type: 'text-start', id: 'txt-1' }),
|
|
2436
|
+
formatChunk({
|
|
2437
|
+
type: 'text-delta',
|
|
2438
|
+
id: 'txt-1',
|
|
2439
|
+
delta: 'The weather in Tokyo is sunny.',
|
|
2440
|
+
}),
|
|
2441
|
+
formatChunk({ type: 'text-end', id: 'txt-1' }),
|
|
2442
|
+
formatChunk({ type: 'finish-step' }),
|
|
2443
|
+
formatChunk({
|
|
2444
|
+
type: 'finish',
|
|
2445
|
+
finishReason: 'stop',
|
|
2446
|
+
}),
|
|
2447
|
+
],
|
|
2448
|
+
},
|
|
2449
|
+
];
|
|
2450
|
+
|
|
2451
|
+
chat = new TestChat({
|
|
2452
|
+
id: '123',
|
|
2453
|
+
generateId: mockId({ prefix: 'newid' }),
|
|
2454
|
+
transport: new DefaultChatTransport({
|
|
2455
|
+
api: 'http://localhost:3000/api/chat',
|
|
2456
|
+
}),
|
|
2457
|
+
messages: [
|
|
2458
|
+
{
|
|
2459
|
+
id: 'id-0',
|
|
2460
|
+
role: 'user',
|
|
2461
|
+
parts: [{ text: 'What is the weather in Tokyo?', type: 'text' }],
|
|
2462
|
+
},
|
|
2463
|
+
{
|
|
2464
|
+
id: 'id-1',
|
|
2465
|
+
role: 'assistant',
|
|
2466
|
+
parts: [
|
|
2467
|
+
{ type: 'step-start' },
|
|
2468
|
+
{
|
|
2469
|
+
type: 'tool-weather',
|
|
2470
|
+
toolCallId: 'call-1',
|
|
2471
|
+
state: 'approval-requested',
|
|
2472
|
+
input: { city: 'Tokyo' },
|
|
2473
|
+
approval: { id: 'approval-1' },
|
|
2474
|
+
},
|
|
2475
|
+
],
|
|
2476
|
+
},
|
|
2477
|
+
],
|
|
2478
|
+
sendAutomaticallyWhen:
|
|
2479
|
+
lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
2480
|
+
onFinish: () => {
|
|
2481
|
+
onFinishPromise.resolve();
|
|
2482
|
+
},
|
|
2483
|
+
});
|
|
2484
|
+
|
|
2485
|
+
await chat.addToolApprovalResponse({
|
|
2486
|
+
id: 'approval-1',
|
|
2487
|
+
approved: true,
|
|
2488
|
+
});
|
|
2489
|
+
|
|
2490
|
+
await onFinishPromise.promise;
|
|
2491
|
+
});
|
|
2492
|
+
|
|
2493
|
+
it('should update tool invocation to show the approval response', () => {
|
|
2494
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2495
|
+
[
|
|
2496
|
+
{
|
|
2497
|
+
"id": "id-0",
|
|
2498
|
+
"parts": [
|
|
2499
|
+
{
|
|
2500
|
+
"text": "What is the weather in Tokyo?",
|
|
2501
|
+
"type": "text",
|
|
2502
|
+
},
|
|
2503
|
+
],
|
|
2504
|
+
"role": "user",
|
|
2505
|
+
},
|
|
2506
|
+
{
|
|
2507
|
+
"id": "id-1",
|
|
2508
|
+
"parts": [
|
|
2509
|
+
{
|
|
2510
|
+
"type": "step-start",
|
|
2511
|
+
},
|
|
2512
|
+
{
|
|
2513
|
+
"approval": {
|
|
2514
|
+
"approved": true,
|
|
2515
|
+
"id": "approval-1",
|
|
2516
|
+
"reason": undefined,
|
|
2517
|
+
},
|
|
2518
|
+
"errorText": undefined,
|
|
2519
|
+
"input": {
|
|
2520
|
+
"city": "Tokyo",
|
|
2521
|
+
},
|
|
2522
|
+
"output": {
|
|
2523
|
+
"temperature": 72,
|
|
2524
|
+
"weather": "sunny",
|
|
2525
|
+
},
|
|
2526
|
+
"preliminary": undefined,
|
|
2527
|
+
"providerExecuted": undefined,
|
|
2528
|
+
"rawInput": undefined,
|
|
2529
|
+
"state": "output-available",
|
|
2530
|
+
"toolCallId": "call-1",
|
|
2531
|
+
"type": "tool-weather",
|
|
2532
|
+
},
|
|
2533
|
+
{
|
|
2534
|
+
"type": "step-start",
|
|
2535
|
+
},
|
|
2536
|
+
{
|
|
2537
|
+
"providerMetadata": undefined,
|
|
2538
|
+
"state": "done",
|
|
2539
|
+
"text": "The weather in Tokyo is sunny.",
|
|
2540
|
+
"type": "text",
|
|
2541
|
+
},
|
|
2542
|
+
],
|
|
2543
|
+
"role": "assistant",
|
|
2544
|
+
},
|
|
2545
|
+
]
|
|
2546
|
+
`);
|
|
2547
|
+
});
|
|
2548
|
+
});
|
|
2549
|
+
});
|
|
2550
|
+
|
|
2551
|
+
describe('addToolResult', () => {
|
|
2552
|
+
it('should send message when a tool result is submitted', async () => {
|
|
2553
|
+
server.urls['http://localhost:3000/api/chat'].response = [
|
|
2554
|
+
{
|
|
2555
|
+
type: 'stream-chunks',
|
|
2556
|
+
chunks: [
|
|
2557
|
+
formatChunk({ type: 'start' }),
|
|
2558
|
+
formatChunk({ type: 'start-step' }),
|
|
2559
|
+
formatChunk({
|
|
2560
|
+
type: 'tool-input-available',
|
|
2561
|
+
toolCallId: 'tool-call-0',
|
|
2562
|
+
toolName: 'test-tool',
|
|
2563
|
+
input: { testArg: 'test-value' },
|
|
2564
|
+
}),
|
|
2565
|
+
formatChunk({ type: 'finish-step' }),
|
|
2566
|
+
formatChunk({ type: 'finish' }),
|
|
2567
|
+
],
|
|
2568
|
+
},
|
|
2569
|
+
{
|
|
2570
|
+
type: 'stream-chunks',
|
|
2571
|
+
chunks: [
|
|
2572
|
+
formatChunk({ type: 'start' }),
|
|
2573
|
+
formatChunk({ type: 'start-step' }),
|
|
2574
|
+
formatChunk({ type: 'finish-step' }),
|
|
2575
|
+
formatChunk({ type: 'finish' }),
|
|
2576
|
+
],
|
|
2577
|
+
},
|
|
2578
|
+
];
|
|
2579
|
+
|
|
2580
|
+
let callCount = 0;
|
|
2581
|
+
const onFinishPromise = createResolvablePromise<void>();
|
|
2582
|
+
|
|
2583
|
+
const chat = new TestChat({
|
|
2584
|
+
id: '123',
|
|
2585
|
+
generateId: mockId(),
|
|
2586
|
+
transport: new DefaultChatTransport({
|
|
2587
|
+
api: 'http://localhost:3000/api/chat',
|
|
2588
|
+
}),
|
|
2589
|
+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
|
|
2590
|
+
onFinish: () => {
|
|
2591
|
+
callCount++;
|
|
2592
|
+
if (callCount === 2) {
|
|
2593
|
+
onFinishPromise.resolve();
|
|
2594
|
+
}
|
|
2595
|
+
},
|
|
2596
|
+
});
|
|
2597
|
+
|
|
2598
|
+
await chat.sendMessage({
|
|
2599
|
+
text: 'Hello, world!',
|
|
2600
|
+
});
|
|
2601
|
+
|
|
2602
|
+
// user submits the tool output
|
|
2603
|
+
await chat.addToolResult({
|
|
2604
|
+
tool: 'test-tool',
|
|
2605
|
+
toolCallId: 'tool-call-0',
|
|
2606
|
+
output: 'test-output',
|
|
2607
|
+
});
|
|
2608
|
+
|
|
2609
|
+
// UI should show the tool output
|
|
2610
|
+
expect(chat.messages).toMatchInlineSnapshot(`
|
|
2611
|
+
[
|
|
2612
|
+
{
|
|
2613
|
+
"id": "id-0",
|
|
2614
|
+
"metadata": undefined,
|
|
2615
|
+
"parts": [
|
|
2616
|
+
{
|
|
2617
|
+
"text": "Hello, world!",
|
|
2618
|
+
"type": "text",
|
|
2619
|
+
},
|
|
2620
|
+
],
|
|
2621
|
+
"role": "user",
|
|
2622
|
+
},
|
|
2623
|
+
{
|
|
2624
|
+
"id": "id-1",
|
|
2625
|
+
"metadata": undefined,
|
|
2626
|
+
"parts": [
|
|
2627
|
+
{
|
|
2628
|
+
"type": "step-start",
|
|
2629
|
+
},
|
|
2630
|
+
{
|
|
2631
|
+
"errorText": undefined,
|
|
2632
|
+
"input": {
|
|
2633
|
+
"testArg": "test-value",
|
|
2634
|
+
},
|
|
2635
|
+
"output": "test-output",
|
|
2636
|
+
"preliminary": undefined,
|
|
2637
|
+
"providerExecuted": undefined,
|
|
2638
|
+
"rawInput": undefined,
|
|
2639
|
+
"state": "output-available",
|
|
2640
|
+
"title": undefined,
|
|
2641
|
+
"toolCallId": "tool-call-0",
|
|
2642
|
+
"type": "tool-test-tool",
|
|
2643
|
+
},
|
|
2644
|
+
],
|
|
2645
|
+
"role": "assistant",
|
|
2646
|
+
},
|
|
2647
|
+
]
|
|
2648
|
+
`);
|
|
2649
|
+
|
|
2650
|
+
await onFinishPromise.promise;
|
|
2651
|
+
|
|
2652
|
+
// 2nd call should happen after the stream is finished
|
|
2653
|
+
expect(server.calls.length).toBe(2);
|
|
2654
|
+
|
|
2655
|
+
// check details of the 2nd call
|
|
2656
|
+
expect(await server.calls[1].requestBodyJson).toMatchInlineSnapshot(`
|
|
2657
|
+
{
|
|
2658
|
+
"id": "123",
|
|
2659
|
+
"messageId": "id-1",
|
|
2660
|
+
"messages": [
|
|
2661
|
+
{
|
|
2662
|
+
"id": "id-0",
|
|
2663
|
+
"parts": [
|
|
2664
|
+
{
|
|
2665
|
+
"text": "Hello, world!",
|
|
2666
|
+
"type": "text",
|
|
2667
|
+
},
|
|
2668
|
+
],
|
|
2669
|
+
"role": "user",
|
|
2670
|
+
},
|
|
2671
|
+
{
|
|
2672
|
+
"id": "id-1",
|
|
2673
|
+
"parts": [
|
|
2674
|
+
{
|
|
2675
|
+
"type": "step-start",
|
|
2676
|
+
},
|
|
2677
|
+
{
|
|
2678
|
+
"input": {
|
|
2679
|
+
"testArg": "test-value",
|
|
2680
|
+
},
|
|
2681
|
+
"output": "test-output",
|
|
2682
|
+
"state": "output-available",
|
|
2683
|
+
"toolCallId": "tool-call-0",
|
|
2684
|
+
"type": "tool-test-tool",
|
|
2685
|
+
},
|
|
2686
|
+
],
|
|
2687
|
+
"role": "assistant",
|
|
2688
|
+
},
|
|
2689
|
+
],
|
|
2690
|
+
"trigger": "submit-message",
|
|
2691
|
+
}
|
|
2692
|
+
`);
|
|
2693
|
+
});
|
|
2694
|
+
});
|
|
2695
|
+
});
|