@assistant-ui/react 0.7.47 → 0.7.49
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/api/MessageRuntime.d.ts +0 -1
- package/dist/api/MessageRuntime.d.ts.map +1 -1
- package/dist/api/MessageRuntime.js +7 -16
- package/dist/api/MessageRuntime.js.map +1 -1
- package/dist/api/MessageRuntime.mjs +7 -15
- package/dist/api/MessageRuntime.mjs.map +1 -1
- package/dist/api/ThreadRuntime.d.ts.map +1 -1
- package/dist/api/ThreadRuntime.js +2 -0
- package/dist/api/ThreadRuntime.js.map +1 -1
- package/dist/api/ThreadRuntime.mjs +2 -0
- package/dist/api/ThreadRuntime.mjs.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.d.ts.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.js +1 -0
- package/dist/context/providers/AssistantRuntimeProvider.js.map +1 -1
- package/dist/context/providers/AssistantRuntimeProvider.mjs +1 -0
- package/dist/context/providers/AssistantRuntimeProvider.mjs.map +1 -1
- package/dist/context/react/utils/useRuntimeState.d.ts +1 -1
- package/dist/context/react/utils/useRuntimeState.d.ts.map +1 -1
- package/dist/context/react/utils/useRuntimeState.js +1 -1
- package/dist/context/react/utils/useRuntimeState.js.map +1 -1
- package/dist/context/react/utils/useRuntimeState.mjs +1 -1
- package/dist/context/react/utils/useRuntimeState.mjs.map +1 -1
- package/dist/primitives/message/MessageContent.d.ts.map +1 -1
- package/dist/primitives/message/MessageContent.js +18 -6
- package/dist/primitives/message/MessageContent.js.map +1 -1
- package/dist/primitives/message/MessageContent.mjs +18 -6
- package/dist/primitives/message/MessageContent.mjs.map +1 -1
- package/dist/runtimes/external-store/external-message-converter.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-message-converter.js +15 -3
- package/dist/runtimes/external-store/external-message-converter.js.map +1 -1
- package/dist/runtimes/external-store/external-message-converter.mjs +16 -4
- package/dist/runtimes/external-store/external-message-converter.mjs.map +1 -1
- package/dist/runtimes/external-store/getExternalStoreMessage.d.ts +8 -1
- package/dist/runtimes/external-store/getExternalStoreMessage.d.ts.map +1 -1
- package/dist/runtimes/external-store/getExternalStoreMessage.js +10 -0
- package/dist/runtimes/external-store/getExternalStoreMessage.js.map +1 -1
- package/dist/runtimes/external-store/getExternalStoreMessage.mjs +9 -0
- package/dist/runtimes/external-store/getExternalStoreMessage.mjs.map +1 -1
- package/dist/runtimes/local/LocalRuntimeOptions.d.ts +5 -0
- package/dist/runtimes/local/LocalRuntimeOptions.d.ts.map +1 -1
- package/dist/runtimes/local/LocalRuntimeOptions.js.map +1 -1
- package/dist/runtimes/local/LocalRuntimeOptions.mjs.map +1 -1
- package/dist/runtimes/local/LocalThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtimes/local/LocalThreadRuntimeCore.js +8 -2
- package/dist/runtimes/local/LocalThreadRuntimeCore.js.map +1 -1
- package/dist/runtimes/local/LocalThreadRuntimeCore.mjs +8 -2
- package/dist/runtimes/local/LocalThreadRuntimeCore.mjs.map +1 -1
- package/dist/runtimes/local/shouldContinue.d.ts +1 -1
- package/dist/runtimes/local/shouldContinue.d.ts.map +1 -1
- package/dist/runtimes/local/shouldContinue.js +3 -1
- package/dist/runtimes/local/shouldContinue.js.map +1 -1
- package/dist/runtimes/local/shouldContinue.mjs +3 -1
- package/dist/runtimes/local/shouldContinue.mjs.map +1 -1
- package/package.json +1 -1
- package/src/api/MessageRuntime.ts +7 -21
- package/src/api/ThreadRuntime.ts +2 -0
- package/src/context/providers/AssistantRuntimeProvider.tsx +2 -1
- package/src/context/react/utils/useRuntimeState.ts +5 -2
- package/src/primitives/message/MessageContent.tsx +28 -7
- package/src/runtimes/external-store/external-message-converter.tsx +20 -8
- package/src/runtimes/external-store/getExternalStoreMessage.tsx +22 -1
- package/src/runtimes/local/LocalRuntimeOptions.tsx +6 -0
- package/src/runtimes/local/LocalThreadRuntimeCore.tsx +13 -2
- package/src/runtimes/local/shouldContinue.tsx +10 -2
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/LocalRuntimeOptions.tsx"],"sourcesContent":["import { ThreadHistoryAdapter } from \"../adapters/thread-history/ThreadHistoryAdapter\";\nimport { AttachmentAdapter } from \"../adapters/attachment/AttachmentAdapter\";\nimport { ThreadMessageLike } from \"../external-store\";\nimport { FeedbackAdapter } from \"../adapters/feedback/FeedbackAdapter\";\nimport { SpeechSynthesisAdapter } from \"../adapters/speech/SpeechAdapterTypes\";\nimport { ChatModelAdapter } from \"./ChatModelAdapter\";\n\nexport type LocalRuntimeOptionsBase = {\n maxSteps?: number | undefined;\n adapters: {\n chatModel: ChatModelAdapter;\n history?: ThreadHistoryAdapter | undefined;\n attachments?: AttachmentAdapter | undefined;\n speech?: SpeechSynthesisAdapter | undefined;\n feedback?: FeedbackAdapter | undefined;\n };\n};\n\n// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase\nexport type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, \"adapters\"> & {\n initialMessages?: readonly ThreadMessageLike[] | undefined;\n adapters?: Omit<LocalRuntimeOptionsBase[\"adapters\"], \"chatModel\"> | undefined;\n};\n\nexport const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(\n options: T,\n) => {\n const { initialMessages, maxSteps, adapters, ...rest } = options;\n\n return {\n localRuntimeOptions: {\n initialMessages,\n maxSteps,\n adapters,\n },\n otherOptions: rest,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/LocalRuntimeOptions.tsx"],"sourcesContent":["import { ThreadHistoryAdapter } from \"../adapters/thread-history/ThreadHistoryAdapter\";\nimport { AttachmentAdapter } from \"../adapters/attachment/AttachmentAdapter\";\nimport { ThreadMessageLike } from \"../external-store\";\nimport { FeedbackAdapter } from \"../adapters/feedback/FeedbackAdapter\";\nimport { SpeechSynthesisAdapter } from \"../adapters/speech/SpeechAdapterTypes\";\nimport { ChatModelAdapter } from \"./ChatModelAdapter\";\n\nexport type LocalRuntimeOptionsBase = {\n maxSteps?: number | undefined;\n adapters: {\n chatModel: ChatModelAdapter;\n history?: ThreadHistoryAdapter | undefined;\n attachments?: AttachmentAdapter | undefined;\n speech?: SpeechSynthesisAdapter | undefined;\n feedback?: FeedbackAdapter | undefined;\n };\n\n /**\n * @deprecated This is a temporary workaround for subgraph frontend function calls.\n * This feature will be removed in a future version without notice. DO NOT USE.\n */\n unstable_shouldContinueIgnoreToolNames?: string[] | undefined;\n};\n\n// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase\nexport type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, \"adapters\"> & {\n initialMessages?: readonly ThreadMessageLike[] | undefined;\n adapters?: Omit<LocalRuntimeOptionsBase[\"adapters\"], \"chatModel\"> | undefined;\n};\n\nexport const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(\n options: T,\n) => {\n const { initialMessages, maxSteps, adapters, ...rest } = options;\n\n return {\n localRuntimeOptions: {\n initialMessages,\n maxSteps,\n adapters,\n },\n otherOptions: rest,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BO,IAAM,2BAA2B,CACtC,YACG;AACH,QAAM,EAAE,iBAAiB,UAAU,UAAU,GAAG,KAAK,IAAI;AAEzD,SAAO;AAAA,IACL,qBAAqB;AAAA,MACnB;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,cAAc;AAAA,EAChB;AACF;","names":[]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/LocalRuntimeOptions.tsx"],"sourcesContent":["import { ThreadHistoryAdapter } from \"../adapters/thread-history/ThreadHistoryAdapter\";\nimport { AttachmentAdapter } from \"../adapters/attachment/AttachmentAdapter\";\nimport { ThreadMessageLike } from \"../external-store\";\nimport { FeedbackAdapter } from \"../adapters/feedback/FeedbackAdapter\";\nimport { SpeechSynthesisAdapter } from \"../adapters/speech/SpeechAdapterTypes\";\nimport { ChatModelAdapter } from \"./ChatModelAdapter\";\n\nexport type LocalRuntimeOptionsBase = {\n maxSteps?: number | undefined;\n adapters: {\n chatModel: ChatModelAdapter;\n history?: ThreadHistoryAdapter | undefined;\n attachments?: AttachmentAdapter | undefined;\n speech?: SpeechSynthesisAdapter | undefined;\n feedback?: FeedbackAdapter | undefined;\n };\n};\n\n// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase\nexport type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, \"adapters\"> & {\n initialMessages?: readonly ThreadMessageLike[] | undefined;\n adapters?: Omit<LocalRuntimeOptionsBase[\"adapters\"], \"chatModel\"> | undefined;\n};\n\nexport const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(\n options: T,\n) => {\n const { initialMessages, maxSteps, adapters, ...rest } = options;\n\n return {\n localRuntimeOptions: {\n initialMessages,\n maxSteps,\n adapters,\n },\n otherOptions: rest,\n };\n};\n"],"mappings":";
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/LocalRuntimeOptions.tsx"],"sourcesContent":["import { ThreadHistoryAdapter } from \"../adapters/thread-history/ThreadHistoryAdapter\";\nimport { AttachmentAdapter } from \"../adapters/attachment/AttachmentAdapter\";\nimport { ThreadMessageLike } from \"../external-store\";\nimport { FeedbackAdapter } from \"../adapters/feedback/FeedbackAdapter\";\nimport { SpeechSynthesisAdapter } from \"../adapters/speech/SpeechAdapterTypes\";\nimport { ChatModelAdapter } from \"./ChatModelAdapter\";\n\nexport type LocalRuntimeOptionsBase = {\n maxSteps?: number | undefined;\n adapters: {\n chatModel: ChatModelAdapter;\n history?: ThreadHistoryAdapter | undefined;\n attachments?: AttachmentAdapter | undefined;\n speech?: SpeechSynthesisAdapter | undefined;\n feedback?: FeedbackAdapter | undefined;\n };\n\n /**\n * @deprecated This is a temporary workaround for subgraph frontend function calls.\n * This feature will be removed in a future version without notice. DO NOT USE.\n */\n unstable_shouldContinueIgnoreToolNames?: string[] | undefined;\n};\n\n// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase\nexport type LocalRuntimeOptions = Omit<LocalRuntimeOptionsBase, \"adapters\"> & {\n initialMessages?: readonly ThreadMessageLike[] | undefined;\n adapters?: Omit<LocalRuntimeOptionsBase[\"adapters\"], \"chatModel\"> | undefined;\n};\n\nexport const splitLocalRuntimeOptions = <T extends LocalRuntimeOptions>(\n options: T,\n) => {\n const { initialMessages, maxSteps, adapters, ...rest } = options;\n\n return {\n localRuntimeOptions: {\n initialMessages,\n maxSteps,\n adapters,\n },\n otherOptions: rest,\n };\n};\n"],"mappings":";AA8BO,IAAM,2BAA2B,CACtC,YACG;AACH,QAAM,EAAE,iBAAiB,UAAU,UAAU,GAAG,KAAK,IAAI;AAEzD,SAAO;AAAA,IACL,qBAAqB;AAAA,MACnB;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,cAAc;AAAA,EAChB;AACF;","names":[]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"LocalThreadRuntimeCore.d.ts","sourceRoot":"","sources":["../../../src/runtimes/local/LocalThreadRuntimeCore.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EACnB,aAAa,EAEd,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,EACf,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAGtE,qBAAa,sBACX,SAAQ,qBACR,YAAW,iBAAiB;IAE5B,SAAgB,YAAY;;;;;;;;;MAS1B;IAEF,OAAO,CAAC,eAAe,CAAgC;IAEvD,SAAgB,UAAU,SAAS;IACnC,SAAgB,WAAW,EAAE,SAAS,gBAAgB,EAAE,CAAM;IAE9D,IAAW,QAAQ;;;;;;MAElB;gBAGC,cAAc,EAAE,mBAAmB,EAC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AE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;YAgCnB,gBAAgB;IAkHvB,SAAS;IAKT,aAAa,CAAC,EACnB,SAAS,EACT,UAAU,EACV,MAAM,GACP,EAAE,oBAAoB;CAwCxB"}
|
@@ -111,7 +111,10 @@ var LocalThreadRuntimeCore = class extends import_BaseThreadRuntimeCore.BaseThre
|
|
111
111
|
try {
|
112
112
|
do {
|
113
113
|
message = await this.performRoundtrip(parentId, message, runConfig);
|
114
|
-
} while ((0, import_shouldContinue.shouldContinue)(
|
114
|
+
} while ((0, import_shouldContinue.shouldContinue)(
|
115
|
+
message,
|
116
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? []
|
117
|
+
));
|
115
118
|
} finally {
|
116
119
|
this._notifyEventSubscribers("run-end");
|
117
120
|
}
|
@@ -234,7 +237,10 @@ var LocalThreadRuntimeCore = class extends import_BaseThreadRuntimeCore.BaseThre
|
|
234
237
|
content: newContent
|
235
238
|
};
|
236
239
|
this.repository.addOrUpdateMessage(parentId, message);
|
237
|
-
if (added && (0, import_shouldContinue.shouldContinue)(
|
240
|
+
if (added && (0, import_shouldContinue.shouldContinue)(
|
241
|
+
message,
|
242
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? []
|
243
|
+
)) {
|
238
244
|
this.performRoundtrip(parentId, message, this._lastRunConfig);
|
239
245
|
}
|
240
246
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/LocalThreadRuntimeCore.tsx"],"sourcesContent":["import { generateId } from \"../../internal\";\nimport type {\n ModelConfigProvider,\n AppendMessage,\n ThreadAssistantMessage,\n} from \"../../types\";\nimport { fromCoreMessage } from \"../edge\";\nimport type { ChatModelRunResult } from \"./ChatModelAdapter\";\nimport { shouldContinue } from \"./shouldContinue\";\nimport { LocalRuntimeOptionsBase } from \"./LocalRuntimeOptions\";\nimport {\n AddToolResultOptions,\n ThreadSuggestion,\n ThreadRuntimeCore,\n StartRunConfig,\n} from \"../core/ThreadRuntimeCore\";\nimport { BaseThreadRuntimeCore } from \"../core/BaseThreadRuntimeCore\";\nimport { RunConfig } from \"../../types/AssistantTypes\";\n\nexport class LocalThreadRuntimeCore\n extends BaseThreadRuntimeCore\n implements ThreadRuntimeCore\n{\n public readonly capabilities = {\n switchToBranch: true,\n edit: true,\n reload: true,\n cancel: true,\n unstable_copy: true,\n speech: false,\n attachments: false,\n feedback: false,\n };\n\n private abortController: AbortController | null = null;\n\n public readonly isDisabled = false;\n public readonly suggestions: readonly ThreadSuggestion[] = [];\n\n public get adapters() {\n return this._options.adapters;\n }\n\n constructor(\n configProvider: ModelConfigProvider,\n options: LocalRuntimeOptionsBase,\n ) {\n super(configProvider);\n this.__internal_setOptions(options);\n }\n\n private _options!: LocalRuntimeOptionsBase;\n\n private _lastRunConfig: RunConfig = {};\n\n public get extras() {\n return undefined;\n }\n\n public __internal_setOptions(options: LocalRuntimeOptionsBase) {\n if (this._options === options) return;\n\n this._options = options;\n\n let hasUpdates = false;\n\n const canSpeak = options.adapters?.speech !== undefined;\n if (this.capabilities.speech !== canSpeak) {\n this.capabilities.speech = canSpeak;\n hasUpdates = true;\n }\n\n const canAttach = options.adapters?.attachments !== undefined;\n if (this.capabilities.attachments !== canAttach) {\n this.capabilities.attachments = canAttach;\n hasUpdates = true;\n }\n\n const canFeedback = options.adapters?.feedback !== undefined;\n if (this.capabilities.feedback !== canFeedback) {\n this.capabilities.feedback = canFeedback;\n hasUpdates = true;\n }\n\n if (hasUpdates) this._notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n this.ensureInitialized();\n\n const newMessage = fromCoreMessage(message, {\n attachments: message.attachments,\n });\n this.repository.addOrUpdateMessage(message.parentId, newMessage);\n\n const startRun = message.startRun ?? message.role === \"user\";\n if (startRun) {\n await this.startRun({\n parentId: newMessage.id,\n sourceId: message.sourceId,\n runConfig: message.runConfig ?? {},\n });\n } else {\n this.repository.resetHead(newMessage.id);\n this._notifySubscribers();\n }\n }\n\n public async startRun({\n parentId,\n runConfig,\n }: StartRunConfig): Promise<void> {\n this.ensureInitialized();\n\n this.repository.resetHead(parentId);\n\n // add assistant message\n const id = generateId();\n let message: ThreadAssistantMessage = {\n id,\n role: \"assistant\",\n status: { type: \"running\" },\n content: [],\n metadata: { unstable_data: [], steps: [], custom: {} },\n createdAt: new Date(),\n };\n\n this._notifyEventSubscribers(\"run-start\");\n\n try {\n do {\n message = await this.performRoundtrip(parentId, message, runConfig);\n } while (shouldContinue(message));\n } finally {\n this._notifyEventSubscribers(\"run-end\");\n }\n }\n\n private async performRoundtrip(\n parentId: string | null,\n message: ThreadAssistantMessage,\n runConfig: RunConfig | undefined,\n ) {\n const messages = this.repository.getMessages();\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n const initialContent = message.content;\n const initialData = message.metadata?.unstable_data;\n const initialSteps = message.metadata?.steps;\n const initalCustom = message.metadata?.custom;\n const updateMessage = (m: Partial<ChatModelRunResult>) => {\n const newSteps = m.metadata?.steps;\n const steps = newSteps\n ? [...(initialSteps ?? []), ...newSteps]\n : undefined;\n\n const newData = m.metadata?.unstable_data;\n const data = newData ? [...(initialData ?? []), ...newData] : undefined;\n\n message = {\n ...message,\n ...(m.content\n ? { content: [...initialContent, ...(m.content ?? [])] }\n : undefined),\n status: m.status ?? message.status,\n ...(m.metadata\n ? {\n metadata: {\n ...message.metadata,\n ...(data ? { unstable_data: data } : undefined),\n ...(steps ? { steps } : undefined),\n ...(m.metadata?.custom\n ? {\n custom: { ...(initalCustom ?? {}), ...m.metadata.custom },\n }\n : undefined),\n },\n }\n : undefined),\n };\n this.repository.addOrUpdateMessage(parentId, message);\n this._notifySubscribers();\n };\n\n const maxSteps = this._options.maxSteps ?? 2;\n\n const steps = message.metadata?.steps?.length ?? 0;\n if (steps >= maxSteps) {\n // reached max tool steps\n updateMessage({\n status: {\n type: \"incomplete\",\n reason: \"tool-calls\",\n },\n });\n return message;\n } else {\n updateMessage({\n status: {\n type: \"running\",\n },\n });\n }\n\n try {\n this._lastRunConfig = runConfig ?? {};\n const promiseOrGenerator = this.adapters.chatModel.run({\n messages,\n runConfig: this._lastRunConfig,\n abortSignal: this.abortController.signal,\n config: this.getModelConfig(),\n unstable_assistantMessageId: message.id,\n });\n\n // handle async iterator for streaming results\n if (Symbol.asyncIterator in promiseOrGenerator) {\n for await (const r of promiseOrGenerator) {\n updateMessage(r);\n }\n } else {\n updateMessage(await promiseOrGenerator);\n }\n\n this.abortController = null;\n\n if (message.status.type === \"running\") {\n updateMessage({\n status: { type: \"complete\", reason: \"unknown\" },\n });\n }\n } catch (e) {\n this.abortController = null;\n\n // TODO this should be handled by the run result stream\n if (e instanceof Error && e.name === \"AbortError\") {\n updateMessage({\n status: { type: \"incomplete\", reason: \"cancelled\" },\n });\n } else {\n updateMessage({\n status: { type: \"incomplete\", reason: \"error\", error: e },\n });\n\n throw e;\n }\n }\n return message;\n }\n\n public cancelRun() {\n this.abortController?.abort();\n this.abortController = null;\n }\n\n public addToolResult({\n messageId,\n toolCallId,\n result,\n }: AddToolResultOptions) {\n const messageData = this.repository.getMessage(messageId);\n const { parentId } = messageData;\n let { message } = messageData;\n\n if (message.role !== \"assistant\")\n throw new Error(\"Tried to add tool result to non-assistant message\");\n\n let added = false;\n let found = false;\n const newContent = message.content.map((c) => {\n if (c.type !== \"tool-call\") return c;\n if (c.toolCallId !== toolCallId) return c;\n found = true;\n if (!c.result) added = true;\n return {\n ...c,\n result,\n };\n });\n\n if (!found)\n throw new Error(\"Tried to add tool result to non-existing tool call\");\n\n message = {\n ...message,\n content: newContent,\n };\n this.repository.addOrUpdateMessage(parentId, message);\n\n if (added && shouldContinue(message)) {\n this.performRoundtrip(parentId, message, this._lastRunConfig);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAA2B;AAM3B,kBAAgC;AAEhC,4BAA+B;AAQ/B,mCAAsC;AAG/B,IAAM,yBAAN,cACG,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MAAM,oDAAoD;AAEtE,cAAU;AAAA,MACR,GAAG;AAAA,MACH,SAAS;AAAA,IACX;AACA,SAAK,WAAW,mBAAmB,UAAU,OAAO;AAEpD,QAAI,aAAS,sCAAe,OAAO,GAAG;AACpC,WAAK,iBAAiB,UAAU,SAAS,KAAK,cAAc;AAAA,IAC9D;AAAA,EACF;AACF;","names":["steps"]}
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/LocalThreadRuntimeCore.tsx"],"sourcesContent":["import { generateId } from \"../../internal\";\nimport type {\n ModelConfigProvider,\n AppendMessage,\n ThreadAssistantMessage,\n} from \"../../types\";\nimport { fromCoreMessage } from \"../edge\";\nimport type { ChatModelRunResult } from \"./ChatModelAdapter\";\nimport { shouldContinue } from \"./shouldContinue\";\nimport { LocalRuntimeOptionsBase } from \"./LocalRuntimeOptions\";\nimport {\n AddToolResultOptions,\n ThreadSuggestion,\n ThreadRuntimeCore,\n StartRunConfig,\n} from \"../core/ThreadRuntimeCore\";\nimport { BaseThreadRuntimeCore } from \"../core/BaseThreadRuntimeCore\";\nimport { RunConfig } from \"../../types/AssistantTypes\";\n\nexport class LocalThreadRuntimeCore\n extends BaseThreadRuntimeCore\n implements ThreadRuntimeCore\n{\n public readonly capabilities = {\n switchToBranch: true,\n edit: true,\n reload: true,\n cancel: true,\n unstable_copy: true,\n speech: false,\n attachments: false,\n feedback: false,\n };\n\n private abortController: AbortController | null = null;\n\n public readonly isDisabled = false;\n public readonly suggestions: readonly ThreadSuggestion[] = [];\n\n public get adapters() {\n return this._options.adapters;\n }\n\n constructor(\n configProvider: ModelConfigProvider,\n options: LocalRuntimeOptionsBase,\n ) {\n super(configProvider);\n this.__internal_setOptions(options);\n }\n\n private _options!: LocalRuntimeOptionsBase;\n\n private _lastRunConfig: RunConfig = {};\n\n public get extras() {\n return undefined;\n }\n\n public __internal_setOptions(options: LocalRuntimeOptionsBase) {\n if (this._options === options) return;\n\n this._options = options;\n\n let hasUpdates = false;\n\n const canSpeak = options.adapters?.speech !== undefined;\n if (this.capabilities.speech !== canSpeak) {\n this.capabilities.speech = canSpeak;\n hasUpdates = true;\n }\n\n const canAttach = options.adapters?.attachments !== undefined;\n if (this.capabilities.attachments !== canAttach) {\n this.capabilities.attachments = canAttach;\n hasUpdates = true;\n }\n\n const canFeedback = options.adapters?.feedback !== undefined;\n if (this.capabilities.feedback !== canFeedback) {\n this.capabilities.feedback = canFeedback;\n hasUpdates = true;\n }\n\n if (hasUpdates) this._notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n this.ensureInitialized();\n\n const newMessage = fromCoreMessage(message, {\n attachments: message.attachments,\n });\n this.repository.addOrUpdateMessage(message.parentId, newMessage);\n\n const startRun = message.startRun ?? message.role === \"user\";\n if (startRun) {\n await this.startRun({\n parentId: newMessage.id,\n sourceId: message.sourceId,\n runConfig: message.runConfig ?? {},\n });\n } else {\n this.repository.resetHead(newMessage.id);\n this._notifySubscribers();\n }\n }\n\n public async startRun({\n parentId,\n runConfig,\n }: StartRunConfig): Promise<void> {\n this.ensureInitialized();\n\n this.repository.resetHead(parentId);\n\n // add assistant message\n const id = generateId();\n let message: ThreadAssistantMessage = {\n id,\n role: \"assistant\",\n status: { type: \"running\" },\n content: [],\n metadata: { unstable_data: [], steps: [], custom: {} },\n createdAt: new Date(),\n };\n\n this._notifyEventSubscribers(\"run-start\");\n\n try {\n do {\n message = await this.performRoundtrip(parentId, message, runConfig);\n } while (\n shouldContinue(\n message,\n this._options.unstable_shouldContinueIgnoreToolNames ?? [],\n )\n );\n } finally {\n this._notifyEventSubscribers(\"run-end\");\n }\n }\n\n private async performRoundtrip(\n parentId: string | null,\n message: ThreadAssistantMessage,\n runConfig: RunConfig | undefined,\n ) {\n const messages = this.repository.getMessages();\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n const initialContent = message.content;\n const initialData = message.metadata?.unstable_data;\n const initialSteps = message.metadata?.steps;\n const initalCustom = message.metadata?.custom;\n const updateMessage = (m: Partial<ChatModelRunResult>) => {\n const newSteps = m.metadata?.steps;\n const steps = newSteps\n ? [...(initialSteps ?? []), ...newSteps]\n : undefined;\n\n const newData = m.metadata?.unstable_data;\n const data = newData ? [...(initialData ?? []), ...newData] : undefined;\n\n message = {\n ...message,\n ...(m.content\n ? { content: [...initialContent, ...(m.content ?? [])] }\n : undefined),\n status: m.status ?? message.status,\n ...(m.metadata\n ? {\n metadata: {\n ...message.metadata,\n ...(data ? { unstable_data: data } : undefined),\n ...(steps ? { steps } : undefined),\n ...(m.metadata?.custom\n ? {\n custom: { ...(initalCustom ?? {}), ...m.metadata.custom },\n }\n : undefined),\n },\n }\n : undefined),\n };\n this.repository.addOrUpdateMessage(parentId, message);\n this._notifySubscribers();\n };\n\n const maxSteps = this._options.maxSteps ?? 2;\n\n const steps = message.metadata?.steps?.length ?? 0;\n if (steps >= maxSteps) {\n // reached max tool steps\n updateMessage({\n status: {\n type: \"incomplete\",\n reason: \"tool-calls\",\n },\n });\n return message;\n } else {\n updateMessage({\n status: {\n type: \"running\",\n },\n });\n }\n\n try {\n this._lastRunConfig = runConfig ?? {};\n const promiseOrGenerator = this.adapters.chatModel.run({\n messages,\n runConfig: this._lastRunConfig,\n abortSignal: this.abortController.signal,\n config: this.getModelConfig(),\n unstable_assistantMessageId: message.id,\n });\n\n // handle async iterator for streaming results\n if (Symbol.asyncIterator in promiseOrGenerator) {\n for await (const r of promiseOrGenerator) {\n updateMessage(r);\n }\n } else {\n updateMessage(await promiseOrGenerator);\n }\n\n this.abortController = null;\n\n if (message.status.type === \"running\") {\n updateMessage({\n status: { type: \"complete\", reason: \"unknown\" },\n });\n }\n } catch (e) {\n this.abortController = null;\n\n // TODO this should be handled by the run result stream\n if (e instanceof Error && e.name === \"AbortError\") {\n updateMessage({\n status: { type: \"incomplete\", reason: \"cancelled\" },\n });\n } else {\n updateMessage({\n status: { type: \"incomplete\", reason: \"error\", error: e },\n });\n\n throw e;\n }\n }\n return message;\n }\n\n public cancelRun() {\n this.abortController?.abort();\n this.abortController = null;\n }\n\n public addToolResult({\n messageId,\n toolCallId,\n result,\n }: AddToolResultOptions) {\n const messageData = this.repository.getMessage(messageId);\n const { parentId } = messageData;\n let { message } = messageData;\n\n if (message.role !== \"assistant\")\n throw new Error(\"Tried to add tool result to non-assistant message\");\n\n let added = false;\n let found = false;\n const newContent = message.content.map((c) => {\n if (c.type !== \"tool-call\") return c;\n if (c.toolCallId !== toolCallId) return c;\n found = true;\n if (!c.result) added = true;\n return {\n ...c,\n result,\n };\n });\n\n if (!found)\n throw new Error(\"Tried to add tool result to non-existing tool call\");\n\n message = {\n ...message,\n content: newContent,\n };\n this.repository.addOrUpdateMessage(parentId, message);\n\n if (\n added &&\n shouldContinue(\n message,\n this._options.unstable_shouldContinueIgnoreToolNames ?? [],\n )\n ) {\n this.performRoundtrip(parentId, message, this._lastRunConfig);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAA2B;AAM3B,kBAAgC;AAEhC,4BAA+B;AAQ/B,mCAAsC;AAG/B,IAAM,yBAAN,cACG,mDAEV;AAAA,EACkB,eAAe;AAAA,IAC7B,gBAAgB;AAAA,IAChB,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,eAAe;AAAA,IACf,QAAQ;AAAA,IACR,aAAa;AAAA,IACb,UAAU;AAAA,EACZ;AAAA,EAEQ,kBAA0C;AAAA,EAElC,aAAa;AAAA,EACb,cAA2C,CAAC;AAAA,EAE5D,IAAW,WAAW;AACpB,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA,EAEA,YACE,gBACA,SACA;AACA,UAAM,cAAc;AACpB,SAAK,sBAAsB,OAAO;AAAA,EACpC;AAAA,EAEQ;AAAA,EAEA,iBAA4B,CAAC;AAAA,EAErC,IAAW,SAAS;AAClB,WAAO;AAAA,EACT;AAAA,EAEO,sBAAsB,SAAkC;AAC7D,QAAI,KAAK,aAAa,QAAS;A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Q,EAAE,MAAM,cAAc,QAAQ,SAAS,OAAO,EAAE;AAAA,QAC1D,CAAC;AAED,cAAM;AAAA,MACR;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA,EAEO,YAAY;AACjB,SAAK,iBAAiB,MAAM;AAC5B,SAAK,kBAAkB;AAAA,EACzB;AAAA,EAEO,cAAc;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAyB;AACvB,UAAM,cAAc,KAAK,WAAW,WAAW,SAAS;AACxD,UAAM,EAAE,SAAS,IAAI;AACrB,QAAI,EAAE,QAAQ,IAAI;AAElB,QAAI,QAAQ,SAAS;AACnB,YAAM,IAAI,MAAM,mDAAmD;AAErE,QAAI,QAAQ;AACZ,QAAI,QAAQ;AACZ,UAAM,aAAa,QAAQ,QAAQ,IAAI,CAAC,MAAM;AAC5C,UAAI,EAAE,SAAS,YAAa,QAAO;AACnC,UAAI,EAAE,eAAe,WAAY,QAAO;AACxC,cAAQ;AACR,UAAI,CAAC,EAAE,OAAQ,SAAQ;AACvB,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,MACF;AAAA,IACF,CAAC;AAED,QAAI,CAAC;AACH,YAAM,IAAI,MAAM,oDAAoD;AAEtE,cAAU;AAAA,MACR,GAAG;AAAA,MACH,SAAS;AAAA,IACX;AACA,SAAK,WAAW,mBAAmB,UAAU,OAAO;AAEpD,QACE,aACA;AAAA,MACE;AAAA,MACA,KAAK,SAAS,0CAA0C,CAAC;AAAA,IAC3D,GACA;AACA,WAAK,iBAAiB,UAAU,SAAS,KAAK,cAAc;AAAA,IAC9D;AAAA,EACF;AACF;","names":["steps"]}
|
@@ -87,7 +87,10 @@ var LocalThreadRuntimeCore = class extends BaseThreadRuntimeCore {
|
|
87
87
|
try {
|
88
88
|
do {
|
89
89
|
message = await this.performRoundtrip(parentId, message, runConfig);
|
90
|
-
} while (shouldContinue(
|
90
|
+
} while (shouldContinue(
|
91
|
+
message,
|
92
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? []
|
93
|
+
));
|
91
94
|
} finally {
|
92
95
|
this._notifyEventSubscribers("run-end");
|
93
96
|
}
|
@@ -210,7 +213,10 @@ var LocalThreadRuntimeCore = class extends BaseThreadRuntimeCore {
|
|
210
213
|
content: newContent
|
211
214
|
};
|
212
215
|
this.repository.addOrUpdateMessage(parentId, message);
|
213
|
-
if (added && shouldContinue(
|
216
|
+
if (added && shouldContinue(
|
217
|
+
message,
|
218
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? []
|
219
|
+
)) {
|
214
220
|
this.performRoundtrip(parentId, message, this._lastRunConfig);
|
215
221
|
}
|
216
222
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/LocalThreadRuntimeCore.tsx"],"sourcesContent":["import { generateId } from \"../../internal\";\nimport type {\n ModelConfigProvider,\n AppendMessage,\n ThreadAssistantMessage,\n} from \"../../types\";\nimport { fromCoreMessage } from \"../edge\";\nimport type { ChatModelRunResult } from \"./ChatModelAdapter\";\nimport { shouldContinue } from \"./shouldContinue\";\nimport { LocalRuntimeOptionsBase } from \"./LocalRuntimeOptions\";\nimport {\n AddToolResultOptions,\n ThreadSuggestion,\n ThreadRuntimeCore,\n StartRunConfig,\n} from \"../core/ThreadRuntimeCore\";\nimport { BaseThreadRuntimeCore } from \"../core/BaseThreadRuntimeCore\";\nimport { RunConfig } from \"../../types/AssistantTypes\";\n\nexport class LocalThreadRuntimeCore\n extends BaseThreadRuntimeCore\n implements ThreadRuntimeCore\n{\n public readonly capabilities = {\n switchToBranch: true,\n edit: true,\n reload: true,\n cancel: true,\n unstable_copy: true,\n speech: false,\n attachments: false,\n feedback: false,\n };\n\n private abortController: AbortController | null = null;\n\n public readonly isDisabled = false;\n public readonly suggestions: readonly ThreadSuggestion[] = [];\n\n public get adapters() {\n return this._options.adapters;\n }\n\n constructor(\n configProvider: ModelConfigProvider,\n options: LocalRuntimeOptionsBase,\n ) {\n super(configProvider);\n this.__internal_setOptions(options);\n }\n\n private _options!: LocalRuntimeOptionsBase;\n\n private _lastRunConfig: RunConfig = {};\n\n public get extras() {\n return undefined;\n }\n\n public __internal_setOptions(options: LocalRuntimeOptionsBase) {\n if (this._options === options) return;\n\n this._options = options;\n\n let hasUpdates = false;\n\n const canSpeak = options.adapters?.speech !== undefined;\n if (this.capabilities.speech !== canSpeak) {\n this.capabilities.speech = canSpeak;\n hasUpdates = true;\n }\n\n const canAttach = options.adapters?.attachments !== undefined;\n if (this.capabilities.attachments !== canAttach) {\n this.capabilities.attachments = canAttach;\n hasUpdates = true;\n }\n\n const canFeedback = options.adapters?.feedback !== undefined;\n if (this.capabilities.feedback !== canFeedback) {\n this.capabilities.feedback = canFeedback;\n hasUpdates = true;\n }\n\n if (hasUpdates) this._notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n this.ensureInitialized();\n\n const newMessage = fromCoreMessage(message, {\n attachments: message.attachments,\n });\n this.repository.addOrUpdateMessage(message.parentId, newMessage);\n\n const startRun = message.startRun ?? message.role === \"user\";\n if (startRun) {\n await this.startRun({\n parentId: newMessage.id,\n sourceId: message.sourceId,\n runConfig: message.runConfig ?? {},\n });\n } else {\n this.repository.resetHead(newMessage.id);\n this._notifySubscribers();\n }\n }\n\n public async startRun({\n parentId,\n runConfig,\n }: StartRunConfig): Promise<void> {\n this.ensureInitialized();\n\n this.repository.resetHead(parentId);\n\n // add assistant message\n const id = generateId();\n let message: ThreadAssistantMessage = {\n id,\n role: \"assistant\",\n status: { type: \"running\" },\n content: [],\n metadata: { unstable_data: [], steps: [], custom: {} },\n createdAt: new Date(),\n };\n\n this._notifyEventSubscribers(\"run-start\");\n\n try {\n do {\n message = await this.performRoundtrip(parentId, message, runConfig);\n } while (shouldContinue(message));\n } finally {\n this._notifyEventSubscribers(\"run-end\");\n }\n }\n\n private async performRoundtrip(\n parentId: string | null,\n message: ThreadAssistantMessage,\n runConfig: RunConfig | undefined,\n ) {\n const messages = this.repository.getMessages();\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n const initialContent = message.content;\n const initialData = message.metadata?.unstable_data;\n const initialSteps = message.metadata?.steps;\n const initalCustom = message.metadata?.custom;\n const updateMessage = (m: Partial<ChatModelRunResult>) => {\n const newSteps = m.metadata?.steps;\n const steps = newSteps\n ? [...(initialSteps ?? []), ...newSteps]\n : undefined;\n\n const newData = m.metadata?.unstable_data;\n const data = newData ? [...(initialData ?? []), ...newData] : undefined;\n\n message = {\n ...message,\n ...(m.content\n ? { content: [...initialContent, ...(m.content ?? [])] }\n : undefined),\n status: m.status ?? message.status,\n ...(m.metadata\n ? {\n metadata: {\n ...message.metadata,\n ...(data ? { unstable_data: data } : undefined),\n ...(steps ? { steps } : undefined),\n ...(m.metadata?.custom\n ? {\n custom: { ...(initalCustom ?? {}), ...m.metadata.custom },\n }\n : undefined),\n },\n }\n : undefined),\n };\n this.repository.addOrUpdateMessage(parentId, message);\n this._notifySubscribers();\n };\n\n const maxSteps = this._options.maxSteps ?? 2;\n\n const steps = message.metadata?.steps?.length ?? 0;\n if (steps >= maxSteps) {\n // reached max tool steps\n updateMessage({\n status: {\n type: \"incomplete\",\n reason: \"tool-calls\",\n },\n });\n return message;\n } else {\n updateMessage({\n status: {\n type: \"running\",\n },\n });\n }\n\n try {\n this._lastRunConfig = runConfig ?? {};\n const promiseOrGenerator = this.adapters.chatModel.run({\n messages,\n runConfig: this._lastRunConfig,\n abortSignal: this.abortController.signal,\n config: this.getModelConfig(),\n unstable_assistantMessageId: message.id,\n });\n\n // handle async iterator for streaming results\n if (Symbol.asyncIterator in promiseOrGenerator) {\n for await (const r of promiseOrGenerator) {\n updateMessage(r);\n }\n } else {\n updateMessage(await promiseOrGenerator);\n }\n\n this.abortController = null;\n\n if (message.status.type === \"running\") {\n updateMessage({\n status: { type: \"complete\", reason: \"unknown\" },\n });\n }\n } catch (e) {\n this.abortController = null;\n\n // TODO this should be handled by the run result stream\n if (e instanceof Error && e.name === \"AbortError\") {\n updateMessage({\n status: { type: \"incomplete\", reason: \"cancelled\" },\n });\n } else {\n updateMessage({\n status: { type: \"incomplete\", reason: \"error\", error: e },\n });\n\n throw e;\n }\n }\n return message;\n }\n\n public cancelRun() {\n this.abortController?.abort();\n this.abortController = null;\n }\n\n public addToolResult({\n messageId,\n toolCallId,\n result,\n }: AddToolResultOptions) {\n const messageData = this.repository.getMessage(messageId);\n const { parentId } = messageData;\n let { message } = messageData;\n\n if (message.role !== \"assistant\")\n throw new Error(\"Tried to add tool result to non-assistant message\");\n\n let added = false;\n let found = false;\n const newContent = message.content.map((c) => {\n if (c.type !== \"tool-call\") return c;\n if (c.toolCallId !== toolCallId) return c;\n found = true;\n if (!c.result) added = true;\n return {\n ...c,\n result,\n };\n });\n\n if (!found)\n throw new Error(\"Tried to add tool result to non-existing tool call\");\n\n message = {\n ...message,\n content: newContent,\n };\n this.repository.addOrUpdateMessage(parentId, message);\n\n if (added && shouldContinue(message)) {\n this.performRoundtrip(parentId, message, this._lastRunConfig);\n }\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAM3B,SAAS,uBAAuB;AAEhC,SAAS,sBAAsB;AAQ/B,SAAS,6BAA6B;AAG/B,IAAM,yBAAN,cACG,sBAEV;AAAA,EACkB,eAAe;AAAA,IAC7B,gBAAgB;AAAA,IAChB,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,eAAe;AAAA,IACf,QAAQ;AAAA,IACR,aAAa;AAAA,IACb,UAAU;AAAA,EACZ;AAAA,EAEQ,kBAA0C;AAAA,EAElC,aAAa;AAAA,EACb,cAA2C,CAAC;AAAA,EAE5D,IAAW,WAAW;AACpB,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA,EAEA,YACE,gBACA,SACA;AACA,UAAM,cAAc;AACpB,SAAK,sBAAsB,OAAO;AAAA,EACpC;AAAA,EAEQ;AAAA,EAEA,iBAA4B,CAAC;AAAA,EAErC,IAAW,SAAS;AAClB,WAAO;AAAA,E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A,UACZ,QAAQ,EAAE,MAAM,cAAc,QAAQ,SAAS,OAAO,EAAE;AAAA,QAC1D,CAAC;AAED,cAAM;AAAA,MACR;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA,EAEO,YAAY;AACjB,SAAK,iBAAiB,MAAM;AAC5B,SAAK,kBAAkB;AAAA,EACzB;AAAA,EAEO,cAAc;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAyB;AACvB,UAAM,cAAc,KAAK,WAAW,WAAW,SAAS;AACxD,UAAM,EAAE,SAAS,IAAI;AACrB,QAAI,EAAE,QAAQ,IAAI;AAElB,QAAI,QAAQ,SAAS;AACnB,YAAM,IAAI,MAAM,mDAAmD;AAErE,QAAI,QAAQ;AACZ,QAAI,QAAQ;AACZ,UAAM,aAAa,QAAQ,QAAQ,IAAI,CAAC,MAAM;AAC5C,UAAI,EAAE,SAAS,YAAa,QAAO;AACnC,UAAI,EAAE,eAAe,WAAY,QAAO;AACxC,cAAQ;AACR,UAAI,CAAC,EAAE,OAAQ,SAAQ;AACvB,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,MACF;AAAA,IACF,CAAC;AAED,QAAI,CAAC;AACH,YAAM,IAAI,MAAM,oDAAoD;AAEtE,cAAU;AAAA,MACR,GAAG;AAAA,MACH,SAAS;AAAA,IACX;AACA,SAAK,WAAW,mBAAmB,UAAU,OAAO;AAEpD,QAAI,SAAS,eAAe,OAAO,GAAG;AACpC,WAAK,iBAAiB,UAAU,SAAS,KAAK,cAAc;AAAA,IAC9D;AAAA,EACF;AACF;","names":["steps"]}
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/LocalThreadRuntimeCore.tsx"],"sourcesContent":["import { generateId } from \"../../internal\";\nimport type {\n ModelConfigProvider,\n AppendMessage,\n ThreadAssistantMessage,\n} from \"../../types\";\nimport { fromCoreMessage } from \"../edge\";\nimport type { ChatModelRunResult } from \"./ChatModelAdapter\";\nimport { shouldContinue } from \"./shouldContinue\";\nimport { LocalRuntimeOptionsBase } from \"./LocalRuntimeOptions\";\nimport {\n AddToolResultOptions,\n ThreadSuggestion,\n ThreadRuntimeCore,\n StartRunConfig,\n} from \"../core/ThreadRuntimeCore\";\nimport { BaseThreadRuntimeCore } from \"../core/BaseThreadRuntimeCore\";\nimport { RunConfig } from \"../../types/AssistantTypes\";\n\nexport class LocalThreadRuntimeCore\n extends BaseThreadRuntimeCore\n implements ThreadRuntimeCore\n{\n public readonly capabilities = {\n switchToBranch: true,\n edit: true,\n reload: true,\n cancel: true,\n unstable_copy: true,\n speech: false,\n attachments: false,\n feedback: false,\n };\n\n private abortController: AbortController | null = null;\n\n public readonly isDisabled = false;\n public readonly suggestions: readonly ThreadSuggestion[] = [];\n\n public get adapters() {\n return this._options.adapters;\n }\n\n constructor(\n configProvider: ModelConfigProvider,\n options: LocalRuntimeOptionsBase,\n ) {\n super(configProvider);\n this.__internal_setOptions(options);\n }\n\n private _options!: LocalRuntimeOptionsBase;\n\n private _lastRunConfig: RunConfig = {};\n\n public get extras() {\n return undefined;\n }\n\n public __internal_setOptions(options: LocalRuntimeOptionsBase) {\n if (this._options === options) return;\n\n this._options = options;\n\n let hasUpdates = false;\n\n const canSpeak = options.adapters?.speech !== undefined;\n if (this.capabilities.speech !== canSpeak) {\n this.capabilities.speech = canSpeak;\n hasUpdates = true;\n }\n\n const canAttach = options.adapters?.attachments !== undefined;\n if (this.capabilities.attachments !== canAttach) {\n this.capabilities.attachments = canAttach;\n hasUpdates = true;\n }\n\n const canFeedback = options.adapters?.feedback !== undefined;\n if (this.capabilities.feedback !== canFeedback) {\n this.capabilities.feedback = canFeedback;\n hasUpdates = true;\n }\n\n if (hasUpdates) this._notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n this.ensureInitialized();\n\n const newMessage = fromCoreMessage(message, {\n attachments: message.attachments,\n });\n this.repository.addOrUpdateMessage(message.parentId, newMessage);\n\n const startRun = message.startRun ?? message.role === \"user\";\n if (startRun) {\n await this.startRun({\n parentId: newMessage.id,\n sourceId: message.sourceId,\n runConfig: message.runConfig ?? {},\n });\n } else {\n this.repository.resetHead(newMessage.id);\n this._notifySubscribers();\n }\n }\n\n public async startRun({\n parentId,\n runConfig,\n }: StartRunConfig): Promise<void> {\n this.ensureInitialized();\n\n this.repository.resetHead(parentId);\n\n // add assistant message\n const id = generateId();\n let message: ThreadAssistantMessage = {\n id,\n role: \"assistant\",\n status: { type: \"running\" },\n content: [],\n metadata: { unstable_data: [], steps: [], custom: {} },\n createdAt: new Date(),\n };\n\n this._notifyEventSubscribers(\"run-start\");\n\n try {\n do {\n message = await this.performRoundtrip(parentId, message, runConfig);\n } while (\n shouldContinue(\n message,\n this._options.unstable_shouldContinueIgnoreToolNames ?? [],\n )\n );\n } finally {\n this._notifyEventSubscribers(\"run-end\");\n }\n }\n\n private async performRoundtrip(\n parentId: string | null,\n message: ThreadAssistantMessage,\n runConfig: RunConfig | undefined,\n ) {\n const messages = this.repository.getMessages();\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n const initialContent = message.content;\n const initialData = message.metadata?.unstable_data;\n const initialSteps = message.metadata?.steps;\n const initalCustom = message.metadata?.custom;\n const updateMessage = (m: Partial<ChatModelRunResult>) => {\n const newSteps = m.metadata?.steps;\n const steps = newSteps\n ? [...(initialSteps ?? []), ...newSteps]\n : undefined;\n\n const newData = m.metadata?.unstable_data;\n const data = newData ? [...(initialData ?? []), ...newData] : undefined;\n\n message = {\n ...message,\n ...(m.content\n ? { content: [...initialContent, ...(m.content ?? [])] }\n : undefined),\n status: m.status ?? message.status,\n ...(m.metadata\n ? {\n metadata: {\n ...message.metadata,\n ...(data ? { unstable_data: data } : undefined),\n ...(steps ? { steps } : undefined),\n ...(m.metadata?.custom\n ? {\n custom: { ...(initalCustom ?? {}), ...m.metadata.custom },\n }\n : undefined),\n },\n }\n : undefined),\n };\n this.repository.addOrUpdateMessage(parentId, message);\n this._notifySubscribers();\n };\n\n const maxSteps = this._options.maxSteps ?? 2;\n\n const steps = message.metadata?.steps?.length ?? 0;\n if (steps >= maxSteps) {\n // reached max tool steps\n updateMessage({\n status: {\n type: \"incomplete\",\n reason: \"tool-calls\",\n },\n });\n return message;\n } else {\n updateMessage({\n status: {\n type: \"running\",\n },\n });\n }\n\n try {\n this._lastRunConfig = runConfig ?? {};\n const promiseOrGenerator = this.adapters.chatModel.run({\n messages,\n runConfig: this._lastRunConfig,\n abortSignal: this.abortController.signal,\n config: this.getModelConfig(),\n unstable_assistantMessageId: message.id,\n });\n\n // handle async iterator for streaming results\n if (Symbol.asyncIterator in promiseOrGenerator) {\n for await (const r of promiseOrGenerator) {\n updateMessage(r);\n }\n } else {\n updateMessage(await promiseOrGenerator);\n }\n\n this.abortController = null;\n\n if (message.status.type === \"running\") {\n updateMessage({\n status: { type: \"complete\", reason: \"unknown\" },\n });\n }\n } catch (e) {\n this.abortController = null;\n\n // TODO this should be handled by the run result stream\n if (e instanceof Error && e.name === \"AbortError\") {\n updateMessage({\n status: { type: \"incomplete\", reason: \"cancelled\" },\n });\n } else {\n updateMessage({\n status: { type: \"incomplete\", reason: \"error\", error: e },\n });\n\n throw e;\n }\n }\n return message;\n }\n\n public cancelRun() {\n this.abortController?.abort();\n this.abortController = null;\n }\n\n public addToolResult({\n messageId,\n toolCallId,\n result,\n }: AddToolResultOptions) {\n const messageData = this.repository.getMessage(messageId);\n const { parentId } = messageData;\n let { message } = messageData;\n\n if (message.role !== \"assistant\")\n throw new Error(\"Tried to add tool result to non-assistant message\");\n\n let added = false;\n let found = false;\n const newContent = message.content.map((c) => {\n if (c.type !== \"tool-call\") return c;\n if (c.toolCallId !== toolCallId) return c;\n found = true;\n if (!c.result) added = true;\n return {\n ...c,\n result,\n };\n });\n\n if (!found)\n throw new Error(\"Tried to add tool result to non-existing tool call\");\n\n message = {\n ...message,\n content: newContent,\n };\n this.repository.addOrUpdateMessage(parentId, message);\n\n if (\n added &&\n shouldContinue(\n message,\n this._options.unstable_shouldContinueIgnore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AC,MAAM;AAC5C,UAAI,EAAE,SAAS,YAAa,QAAO;AACnC,UAAI,EAAE,eAAe,WAAY,QAAO;AACxC,cAAQ;AACR,UAAI,CAAC,EAAE,OAAQ,SAAQ;AACvB,aAAO;AAAA,QACL,GAAG;AAAA,QACH;AAAA,MACF;AAAA,IACF,CAAC;AAED,QAAI,CAAC;AACH,YAAM,IAAI,MAAM,oDAAoD;AAEtE,cAAU;AAAA,MACR,GAAG;AAAA,MACH,SAAS;AAAA,IACX;AACA,SAAK,WAAW,mBAAmB,UAAU,OAAO;AAEpD,QACE,SACA;AAAA,MACE;AAAA,MACA,KAAK,SAAS,0CAA0C,CAAC;AAAA,IAC3D,GACA;AACA,WAAK,iBAAiB,UAAU,SAAS,KAAK,cAAc;AAAA,IAC9D;AAAA,EACF;AACF;","names":["steps"]}
|
@@ -1,3 +1,3 @@
|
|
1
1
|
import type { ThreadAssistantMessage } from "../../types";
|
2
|
-
export declare const shouldContinue: (result: ThreadAssistantMessage) => boolean;
|
2
|
+
export declare const shouldContinue: (result: ThreadAssistantMessage, ignoreToolNames: string[]) => boolean;
|
3
3
|
//# sourceMappingURL=shouldContinue.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"shouldContinue.d.ts","sourceRoot":"","sources":["../../../src/runtimes/local/shouldContinue.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAE1D,eAAO,MAAM,cAAc,
|
1
|
+
{"version":3,"file":"shouldContinue.d.ts","sourceRoot":"","sources":["../../../src/runtimes/local/shouldContinue.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAE1D,eAAO,MAAM,cAAc,WACjB,sBAAsB,mBACb,MAAM,EAAE,YASxB,CAAC"}
|
@@ -23,7 +23,9 @@ __export(shouldContinue_exports, {
|
|
23
23
|
shouldContinue: () => shouldContinue
|
24
24
|
});
|
25
25
|
module.exports = __toCommonJS(shouldContinue_exports);
|
26
|
-
var shouldContinue = (result) => result.status?.type === "requires-action" && result.status.reason === "tool-calls" && result.content.every(
|
26
|
+
var shouldContinue = (result, ignoreToolNames) => result.status?.type === "requires-action" && result.status.reason === "tool-calls" && result.content.every(
|
27
|
+
(c) => c.type !== "tool-call" || !!c.result || ignoreToolNames.includes(c.toolName)
|
28
|
+
);
|
27
29
|
// Annotate the CommonJS export names for ESM import in node:
|
28
30
|
0 && (module.exports = {
|
29
31
|
shouldContinue
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/shouldContinue.tsx"],"sourcesContent":["import type { ThreadAssistantMessage } from \"../../types\";\n\nexport const shouldContinue = (result: ThreadAssistantMessage) =>\n result.status?.type === \"requires-action\" &&\n result.status.reason === \"tool-calls\" &&\n result.content.every((c)
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/shouldContinue.tsx"],"sourcesContent":["import type { ThreadAssistantMessage } from \"../../types\";\n\nexport const shouldContinue = (\n result: ThreadAssistantMessage,\n ignoreToolNames: string[],\n) =>\n result.status?.type === \"requires-action\" &&\n result.status.reason === \"tool-calls\" &&\n result.content.every(\n (c) =>\n c.type !== \"tool-call\" ||\n !!c.result ||\n ignoreToolNames.includes(c.toolName),\n );\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,IAAM,iBAAiB,CAC5B,QACA,oBAEA,OAAO,QAAQ,SAAS,qBACxB,OAAO,OAAO,WAAW,gBACzB,OAAO,QAAQ;AAAA,EACb,CAAC,MACC,EAAE,SAAS,eACX,CAAC,CAAC,EAAE,UACJ,gBAAgB,SAAS,EAAE,QAAQ;AACvC;","names":[]}
|
@@ -1,5 +1,7 @@
|
|
1
1
|
// src/runtimes/local/shouldContinue.tsx
|
2
|
-
var shouldContinue = (result) => result.status?.type === "requires-action" && result.status.reason === "tool-calls" && result.content.every(
|
2
|
+
var shouldContinue = (result, ignoreToolNames) => result.status?.type === "requires-action" && result.status.reason === "tool-calls" && result.content.every(
|
3
|
+
(c) => c.type !== "tool-call" || !!c.result || ignoreToolNames.includes(c.toolName)
|
4
|
+
);
|
3
5
|
export {
|
4
6
|
shouldContinue
|
5
7
|
};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/runtimes/local/shouldContinue.tsx"],"sourcesContent":["import type { ThreadAssistantMessage } from \"../../types\";\n\nexport const shouldContinue = (result: ThreadAssistantMessage) =>\n result.status?.type === \"requires-action\" &&\n result.status.reason === \"tool-calls\" &&\n result.content.every((c)
|
1
|
+
{"version":3,"sources":["../../../src/runtimes/local/shouldContinue.tsx"],"sourcesContent":["import type { ThreadAssistantMessage } from \"../../types\";\n\nexport const shouldContinue = (\n result: ThreadAssistantMessage,\n ignoreToolNames: string[],\n) =>\n result.status?.type === \"requires-action\" &&\n result.status.reason === \"tool-calls\" &&\n result.content.every(\n (c) =>\n c.type !== \"tool-call\" ||\n !!c.result ||\n ignoreToolNames.includes(c.toolName),\n );\n"],"mappings":";AAEO,IAAM,iBAAiB,CAC5B,QACA,oBAEA,OAAO,QAAQ,SAAS,qBACxB,OAAO,OAAO,WAAW,gBACzB,OAAO,QAAQ;AAAA,EACb,CAAC,MACC,EAAE,SAAS,eACX,CAAC,CAAC,EAAE,UACJ,gBAAgB,SAAS,EAAE,QAAQ;AACvC;","names":[]}
|
package/package.json
CHANGED
@@ -2,6 +2,7 @@ import {
|
|
2
2
|
SpeechState,
|
3
3
|
SubmittedFeedback,
|
4
4
|
} from "../runtimes/core/ThreadRuntimeCore";
|
5
|
+
import { symbolInnerMessage } from "../runtimes/external-store/getExternalStoreMessage";
|
5
6
|
import {
|
6
7
|
ThreadMessage,
|
7
8
|
ThreadAssistantContentPart,
|
@@ -59,37 +60,22 @@ export const toContentPartStatus = (
|
|
59
60
|
return isLastPart ? (message.status as ContentPartStatus) : COMPLETE_STATUS;
|
60
61
|
};
|
61
62
|
|
62
|
-
export const EMPTY_CONTENT_SYMBOL = Symbol("empty-content");
|
63
|
-
const EMPTY_CONTENT = Object.freeze({
|
64
|
-
type: "text",
|
65
|
-
text: "",
|
66
|
-
[EMPTY_CONTENT_SYMBOL]: true,
|
67
|
-
});
|
68
|
-
|
69
63
|
const getContentPartState = (
|
70
64
|
message: MessageState,
|
71
65
|
partIndex: number,
|
72
66
|
): ContentPartState | SKIP_UPDATE => {
|
73
67
|
let part = message.content[partIndex];
|
74
68
|
if (!part) {
|
75
|
-
|
76
|
-
if (message.content.length === 0 && partIndex === 0) {
|
77
|
-
part = EMPTY_CONTENT;
|
78
|
-
} else {
|
79
|
-
return SKIP_UPDATE;
|
80
|
-
}
|
81
|
-
} else if (
|
82
|
-
message.content.length === 1 &&
|
83
|
-
part.type === "text" &&
|
84
|
-
part.text.length === 0
|
85
|
-
) {
|
86
|
-
// ensure reference equality for equivalent empty text parts
|
87
|
-
part = EMPTY_CONTENT;
|
69
|
+
return SKIP_UPDATE;
|
88
70
|
}
|
89
71
|
|
90
72
|
// if the content part is the same, don't update
|
91
73
|
const status = toContentPartStatus(message, partIndex, part);
|
92
|
-
return Object.freeze({
|
74
|
+
return Object.freeze({
|
75
|
+
...part,
|
76
|
+
...{ [symbolInnerMessage]: (part as any)[symbolInnerMessage] },
|
77
|
+
status,
|
78
|
+
});
|
93
79
|
};
|
94
80
|
|
95
81
|
export type MessageState = ThreadMessage & {
|
package/src/api/ThreadRuntime.ts
CHANGED
@@ -35,6 +35,7 @@ import {
|
|
35
35
|
import { ThreadListItemState } from "./ThreadListItemRuntime";
|
36
36
|
import { RunConfig } from "../types/AssistantTypes";
|
37
37
|
import { EventSubscriptionSubject } from "./subscribable/EventSubscriptionSubject";
|
38
|
+
import { symbolInnerMessage } from "../runtimes/external-store/getExternalStoreMessage";
|
38
39
|
|
39
40
|
export type CreateStartRunConfig = {
|
40
41
|
parentId: string | null;
|
@@ -405,6 +406,7 @@ export class ThreadRuntimeImpl implements ThreadRuntime {
|
|
405
406
|
|
406
407
|
return {
|
407
408
|
...message,
|
409
|
+
...{ [symbolInnerMessage]: (message as any)[symbolInnerMessage] },
|
408
410
|
|
409
411
|
isLast: messages.at(-1)?.id === message.id,
|
410
412
|
parentId,
|
@@ -9,8 +9,11 @@ export type SubscribableRuntime<TState> = {
|
|
9
9
|
|
10
10
|
export function useRuntimeStateInternal<TState, TSelected>(
|
11
11
|
runtime: SubscribableRuntime<TState>,
|
12
|
-
selector: ((state: TState) => TSelected) | undefined = identity
|
12
|
+
selector: ((state: TState) => TSelected | TState) | undefined = identity,
|
13
13
|
): TSelected | TState {
|
14
|
+
// TODO move to useRuntimeState
|
15
|
+
ensureBinding(runtime);
|
16
|
+
|
14
17
|
const slice = useSyncExternalStore(
|
15
18
|
runtime.subscribe,
|
16
19
|
() => selector(runtime.getState()),
|
@@ -37,7 +40,7 @@ export function useRuntimeState<TState, TSelected>(
|
|
37
40
|
selector?: ((state: TState) => TSelected) | undefined,
|
38
41
|
): TSelected | TState {
|
39
42
|
// ensure that the runtime is bound
|
40
|
-
ensureBinding(runtime);
|
43
|
+
// ensureBinding(runtime);
|
41
44
|
|
42
45
|
return useRuntimeStateInternal(runtime, selector);
|
43
46
|
}
|
@@ -25,7 +25,7 @@ import type {
|
|
25
25
|
FileContentPartComponent,
|
26
26
|
} from "../../types/ContentPartComponentTypes";
|
27
27
|
import { ContentPartPrimitiveInProgress } from "../contentPart/ContentPartInProgress";
|
28
|
-
import {
|
28
|
+
import { ContentPartStatus } from "../../types/AssistantTypes";
|
29
29
|
|
30
30
|
export namespace MessagePrimitiveContent {
|
31
31
|
export type Props = {
|
@@ -86,7 +86,6 @@ type MessageContentPartComponentProps = {
|
|
86
86
|
const MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({
|
87
87
|
components: {
|
88
88
|
Text = defaultComponents.Text,
|
89
|
-
Empty,
|
90
89
|
Image = defaultComponents.Image,
|
91
90
|
File = defaultComponents.File,
|
92
91
|
Unstable_Audio: Audio = defaultComponents.Unstable_Audio,
|
@@ -112,10 +111,6 @@ const MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({
|
|
112
111
|
|
113
112
|
switch (type) {
|
114
113
|
case "text":
|
115
|
-
if ((part as any)[EMPTY_CONTENT_SYMBOL] && !!Empty) {
|
116
|
-
return <Empty status={part.status} />;
|
117
|
-
}
|
118
|
-
|
119
114
|
return <Text {...part} />;
|
120
115
|
|
121
116
|
case "image":
|
@@ -171,10 +166,36 @@ const MessageContentPart = memo(
|
|
171
166
|
prev.components?.tools === next.components?.tools,
|
172
167
|
);
|
173
168
|
|
169
|
+
const COMPLETE_STATUS: ContentPartStatus = Object.freeze({
|
170
|
+
type: "complete",
|
171
|
+
});
|
172
|
+
|
173
|
+
const EmptyContentImpl: FC<MessageContentPartComponentProps> = ({
|
174
|
+
components,
|
175
|
+
}) => {
|
176
|
+
const status =
|
177
|
+
useMessage((s) => s.status as ContentPartStatus) ?? COMPLETE_STATUS;
|
178
|
+
|
179
|
+
const Component =
|
180
|
+
components?.Empty ?? components?.Text ?? defaultComponents.Text;
|
181
|
+
return <Component type="text" text="" status={status} />;
|
182
|
+
};
|
183
|
+
|
184
|
+
const EmptyContent = memo(
|
185
|
+
EmptyContentImpl,
|
186
|
+
(prev, next) =>
|
187
|
+
prev.components?.Empty === next.components?.Empty &&
|
188
|
+
prev.components?.Text === next.components?.Text,
|
189
|
+
);
|
190
|
+
|
174
191
|
export const MessagePrimitiveContent: FC<MessagePrimitiveContent.Props> = ({
|
175
192
|
components,
|
176
193
|
}) => {
|
177
|
-
const contentLength = useMessage((s) => s.content.length)
|
194
|
+
const contentLength = useMessage((s) => s.content.length);
|
195
|
+
|
196
|
+
if (contentLength === 0) {
|
197
|
+
return <EmptyContent components={components} />;
|
198
|
+
}
|
178
199
|
|
179
200
|
return Array.from({ length: contentLength }, (_, index) => (
|
180
201
|
<MessageContentPart key={index} partIndex={index} components={components} />
|
@@ -3,7 +3,7 @@
|
|
3
3
|
import { useMemo } from "react";
|
4
4
|
import { ThreadMessageConverter } from "./ThreadMessageConverter";
|
5
5
|
import {
|
6
|
-
|
6
|
+
getExternalStoreMessages,
|
7
7
|
symbolInnerMessage,
|
8
8
|
} from "./getExternalStoreMessage";
|
9
9
|
import { fromThreadMessageLike, ThreadMessageLike } from "./ThreadMessageLike";
|
@@ -64,6 +64,12 @@ const joinExternalMessages = (
|
|
64
64
|
}
|
65
65
|
assistantMessage.content[toolCallIdx] = {
|
66
66
|
...toolCall,
|
67
|
+
...{
|
68
|
+
[symbolInnerMessage]: [
|
69
|
+
...((toolCall as any)[symbolInnerMessage] ?? []),
|
70
|
+
output,
|
71
|
+
],
|
72
|
+
},
|
67
73
|
result: output.result,
|
68
74
|
};
|
69
75
|
} else {
|
@@ -73,10 +79,21 @@ const joinExternalMessages = (
|
|
73
79
|
}
|
74
80
|
} else {
|
75
81
|
const role = output.role;
|
82
|
+
const content = (
|
83
|
+
typeof output.content === "string"
|
84
|
+
? [{ type: "text" as const, text: output.content }]
|
85
|
+
: output.content
|
86
|
+
).map((c) => ({
|
87
|
+
...c,
|
88
|
+
...{ [symbolInnerMessage]: [output] },
|
89
|
+
}));
|
76
90
|
switch (role) {
|
77
91
|
case "system":
|
78
92
|
case "user":
|
79
|
-
return
|
93
|
+
return {
|
94
|
+
...output,
|
95
|
+
content,
|
96
|
+
};
|
80
97
|
case "assistant":
|
81
98
|
if (assistantMessage.content.length === 0) {
|
82
99
|
assistantMessage.id = output.id;
|
@@ -114,11 +131,6 @@ const joinExternalMessages = (
|
|
114
131
|
// TODO keep this in sync
|
115
132
|
}
|
116
133
|
|
117
|
-
const content =
|
118
|
-
typeof output.content === "string"
|
119
|
-
? [{ type: "text" as const, text: output.content }]
|
120
|
-
: output.content;
|
121
|
-
|
122
134
|
assistantMessage.content.push(...content);
|
123
135
|
break;
|
124
136
|
default: {
|
@@ -222,7 +234,7 @@ export const useExternalMessageConverter = <T extends WeakKey>({
|
|
222
234
|
!isAutoStatus(cache.status) ||
|
223
235
|
cache.status === autoStatus)
|
224
236
|
) {
|
225
|
-
const inputs =
|
237
|
+
const inputs = getExternalStoreMessages<T>(cache);
|
226
238
|
if (shallowArrayEqual(inputs, message.inputs)) {
|
227
239
|
return cache;
|
228
240
|
}
|
@@ -6,6 +6,27 @@ export type ExternalStoreThreadMessage<T> = ThreadMessage & {
|
|
6
6
|
[symbolInnerMessage]?: T;
|
7
7
|
};
|
8
8
|
|
9
|
-
export
|
9
|
+
export type ExternalStoreThreadMessages<T> = ThreadMessage & {
|
10
|
+
[symbolInnerMessage]?: T[];
|
11
|
+
};
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @deprecated Use `getExternalStoreMessages` (plural) instead. This function will be removed in 0.8.0.
|
15
|
+
*/
|
16
|
+
export const getExternalStoreMessage = <T,>(
|
17
|
+
message: ThreadMessage | ThreadMessage["content"],
|
18
|
+
) => {
|
10
19
|
return (message as ExternalStoreThreadMessage<T>)[symbolInnerMessage];
|
11
20
|
};
|
21
|
+
|
22
|
+
export const getExternalStoreMessages = <T,>(
|
23
|
+
message: ThreadMessage | ThreadMessage["content"],
|
24
|
+
) => {
|
25
|
+
// TODO temp until 0.8.0 (migrate useExternalStoreRuntime to always set an array)
|
26
|
+
const value = (message as ExternalStoreThreadMessages<T>)[symbolInnerMessage];
|
27
|
+
if (!value) return []
|
28
|
+
if (Array.isArray(value)) {
|
29
|
+
return value;
|
30
|
+
}
|
31
|
+
return [value];
|
32
|
+
};
|
@@ -14,6 +14,12 @@ export type LocalRuntimeOptionsBase = {
|
|
14
14
|
speech?: SpeechSynthesisAdapter | undefined;
|
15
15
|
feedback?: FeedbackAdapter | undefined;
|
16
16
|
};
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @deprecated This is a temporary workaround for subgraph frontend function calls.
|
20
|
+
* This feature will be removed in a future version without notice. DO NOT USE.
|
21
|
+
*/
|
22
|
+
unstable_shouldContinueIgnoreToolNames?: string[] | undefined;
|
17
23
|
};
|
18
24
|
|
19
25
|
// TODO align LocalRuntimeOptions with LocalRuntimeOptionsBase
|
@@ -130,7 +130,12 @@ export class LocalThreadRuntimeCore
|
|
130
130
|
try {
|
131
131
|
do {
|
132
132
|
message = await this.performRoundtrip(parentId, message, runConfig);
|
133
|
-
} while (
|
133
|
+
} while (
|
134
|
+
shouldContinue(
|
135
|
+
message,
|
136
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? [],
|
137
|
+
)
|
138
|
+
);
|
134
139
|
} finally {
|
135
140
|
this._notifyEventSubscribers("run-end");
|
136
141
|
}
|
@@ -289,7 +294,13 @@ export class LocalThreadRuntimeCore
|
|
289
294
|
};
|
290
295
|
this.repository.addOrUpdateMessage(parentId, message);
|
291
296
|
|
292
|
-
if (
|
297
|
+
if (
|
298
|
+
added &&
|
299
|
+
shouldContinue(
|
300
|
+
message,
|
301
|
+
this._options.unstable_shouldContinueIgnoreToolNames ?? [],
|
302
|
+
)
|
303
|
+
) {
|
293
304
|
this.performRoundtrip(parentId, message, this._lastRunConfig);
|
294
305
|
}
|
295
306
|
}
|
@@ -1,6 +1,14 @@
|
|
1
1
|
import type { ThreadAssistantMessage } from "../../types";
|
2
2
|
|
3
|
-
export const shouldContinue = (
|
3
|
+
export const shouldContinue = (
|
4
|
+
result: ThreadAssistantMessage,
|
5
|
+
ignoreToolNames: string[],
|
6
|
+
) =>
|
4
7
|
result.status?.type === "requires-action" &&
|
5
8
|
result.status.reason === "tool-calls" &&
|
6
|
-
result.content.every(
|
9
|
+
result.content.every(
|
10
|
+
(c) =>
|
11
|
+
c.type !== "tool-call" ||
|
12
|
+
!!c.result ||
|
13
|
+
ignoreToolNames.includes(c.toolName),
|
14
|
+
);
|