@assistant-ui/react 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -12,8 +12,48 @@ import * as class_variance_authority from 'class-variance-authority';
12
12
  import { VariantProps } from 'class-variance-authority';
13
13
  import * as class_variance_authority_types from 'class-variance-authority/types';
14
14
 
15
+ declare const LanguageModelV1CallSettingsSchema: z.ZodObject<{
16
+ maxTokens: z.ZodOptional<z.ZodNumber>;
17
+ temperature: z.ZodOptional<z.ZodNumber>;
18
+ topP: z.ZodOptional<z.ZodNumber>;
19
+ presencePenalty: z.ZodOptional<z.ZodNumber>;
20
+ frequencyPenalty: z.ZodOptional<z.ZodNumber>;
21
+ seed: z.ZodOptional<z.ZodNumber>;
22
+ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodOptional<z.ZodString>>>;
23
+ }, "strip", z.ZodTypeAny, {
24
+ maxTokens?: number | undefined;
25
+ temperature?: number | undefined;
26
+ topP?: number | undefined;
27
+ presencePenalty?: number | undefined;
28
+ frequencyPenalty?: number | undefined;
29
+ seed?: number | undefined;
30
+ headers?: Record<string, string | undefined> | undefined;
31
+ }, {
32
+ maxTokens?: number | undefined;
33
+ temperature?: number | undefined;
34
+ topP?: number | undefined;
35
+ presencePenalty?: number | undefined;
36
+ frequencyPenalty?: number | undefined;
37
+ seed?: number | undefined;
38
+ headers?: Record<string, string | undefined> | undefined;
39
+ }>;
40
+ type LanguageModelV1CallSettings = z.infer<typeof LanguageModelV1CallSettingsSchema>;
41
+ declare const LanguageModelConfigSchema: z.ZodObject<{
42
+ apiKey: z.ZodOptional<z.ZodString>;
43
+ baseUrl: z.ZodOptional<z.ZodString>;
44
+ modelName: z.ZodOptional<z.ZodString>;
45
+ }, "strip", z.ZodTypeAny, {
46
+ apiKey?: string | undefined;
47
+ baseUrl?: string | undefined;
48
+ modelName?: string | undefined;
49
+ }, {
50
+ apiKey?: string | undefined;
51
+ baseUrl?: string | undefined;
52
+ modelName?: string | undefined;
53
+ }>;
54
+ type LanguageModelConfig = z.infer<typeof LanguageModelConfigSchema>;
15
55
  type ToolExecuteFunction<TArgs, TResult> = (args: TArgs) => TResult | Promise<TResult>;
16
- type Tool<TArgs = unknown, TResult = unknown> = {
56
+ type Tool<TArgs extends Record<string | number, unknown> = Record<string | number, unknown>, TResult = unknown> = {
17
57
  description?: string | undefined;
18
58
  parameters: z.ZodSchema<TArgs> | JSONSchema7;
19
59
  execute?: ToolExecuteFunction<TArgs, TResult>;
@@ -22,6 +62,8 @@ type ModelConfig = {
22
62
  priority?: number | undefined;
23
63
  system?: string | undefined;
24
64
  tools?: Record<string, Tool<any, any>> | undefined;
65
+ callSettings?: LanguageModelV1CallSettings | undefined;
66
+ config?: LanguageModelConfig | undefined;
25
67
  };
26
68
  type ModelConfigProvider = {
27
69
  getModelConfig: () => ModelConfig;
@@ -41,14 +83,16 @@ type UIContentPart = {
41
83
  type: "ui";
42
84
  display: ReactNode;
43
85
  };
44
- type ToolCallContentPart<TArgs = unknown, TResult = unknown> = {
86
+ type CoreToolCallContentPart<TArgs extends Record<string | number, unknown> = Record<string | number, unknown>, TResult = unknown> = {
45
87
  type: "tool-call";
46
88
  toolCallId: string;
47
89
  toolName: string;
48
- argsText: string;
49
90
  args: TArgs;
50
- result?: TResult;
51
- isError?: boolean;
91
+ result?: TResult | undefined;
92
+ isError?: boolean | undefined;
93
+ };
94
+ type ToolCallContentPart<TArgs extends Record<string | number, unknown> = Record<string | number, unknown>, TResult = unknown> = CoreToolCallContentPart<TArgs, TResult> & {
95
+ argsText: string;
52
96
  };
53
97
  type ThreadUserContentPart = TextContentPart | ImageContentPart | UIContentPart;
54
98
  type ThreadAssistantContentPart = TextContentPart | ToolCallContentPart | UIContentPart;
@@ -57,7 +101,7 @@ type MessageCommonProps = {
57
101
  createdAt: Date;
58
102
  };
59
103
  type MessageStatus = {
60
- type: "in_progress";
104
+ type: "in_progress" | "cancelled";
61
105
  } | {
62
106
  type: "done";
63
107
  finishReason?: LanguageModelV1FinishReason;
@@ -89,7 +133,7 @@ type AppendMessage = CoreMessage & {
89
133
  type ThreadMessage = ThreadSystemMessage | ThreadUserMessage | ThreadAssistantMessage;
90
134
  /** Core Message Types (without UI content parts) */
91
135
  type CoreUserContentPart = TextContentPart | ImageContentPart;
92
- type CoreAssistantContentPart = TextContentPart | ToolCallContentPart;
136
+ type CoreAssistantContentPart = TextContentPart | CoreToolCallContentPart;
93
137
  type CoreSystemMessage = {
94
138
  role: "system";
95
139
  content: [TextContentPart];
@@ -189,12 +233,12 @@ type UIContentPartProps = {
189
233
  status: MessageStatus;
190
234
  };
191
235
  type UIContentPartComponent = ComponentType<UIContentPartProps>;
192
- type ToolCallContentPartProps<TArgs = any, TResult = any> = {
236
+ type ToolCallContentPartProps<TArgs extends Record<string | number, unknown> = any, TResult = unknown> = {
193
237
  part: ToolCallContentPart<TArgs, TResult>;
194
238
  status: MessageStatus;
195
239
  addResult: (result: any) => void;
196
240
  };
197
- type ToolCallContentPartComponent<TArgs = any, TResult = any> = ComponentType<ToolCallContentPartProps<TArgs, TResult>>;
241
+ type ToolCallContentPartComponent<TArgs extends Record<string | number, unknown> = any, TResult = any> = ComponentType<ToolCallContentPartProps<TArgs, TResult>>;
198
242
 
199
243
  type EdgeChatAdapterOptions = {
200
244
  api: string;
@@ -380,21 +424,21 @@ declare const useAppendMessage: () => (message: CreateAppendMessage) => void;
380
424
 
381
425
  declare const useSwitchToNewThread: () => () => void;
382
426
 
383
- type AssistantToolProps<TArgs, TResult> = Tool<TArgs, TResult> & {
427
+ type AssistantToolProps<TArgs extends Record<string | number, unknown>, TResult> = Tool<TArgs, TResult> & {
384
428
  toolName: string;
385
429
  render?: ToolCallContentPartComponent<TArgs, TResult> | undefined;
386
430
  };
387
- declare const useAssistantTool: <TArgs, TResult>(tool: AssistantToolProps<TArgs, TResult>) => void;
431
+ declare const useAssistantTool: <TArgs extends Record<string | number, unknown>, TResult>(tool: AssistantToolProps<TArgs, TResult>) => void;
388
432
 
389
- declare const makeAssistantTool: <TArgs, TResult>(tool: AssistantToolProps<TArgs, TResult>) => () => null;
433
+ declare const makeAssistantTool: <TArgs extends Record<string | number, unknown>, TResult>(tool: AssistantToolProps<TArgs, TResult>) => () => null;
390
434
 
391
- type AssistantToolUIProps<TArgs, TResult> = {
435
+ type AssistantToolUIProps<TArgs extends Record<string | number, unknown>, TResult> = {
392
436
  toolName: string;
393
437
  render: ToolCallContentPartComponent<TArgs, TResult>;
394
438
  };
395
439
  declare const useAssistantToolUI: (tool: AssistantToolUIProps<any, any> | null) => void;
396
440
 
397
- declare const makeAssistantToolUI: <TArgs, TResult>(tool: AssistantToolUIProps<TArgs, TResult>) => () => null;
441
+ declare const makeAssistantToolUI: <TArgs extends Record<string | number, unknown>, TResult>(tool: AssistantToolUIProps<TArgs, TResult>) => () => null;
398
442
 
399
443
  declare const useAssistantInstructions: (instruction: string) => void;
400
444
 
package/dist/index.js CHANGED
@@ -116,6 +116,21 @@ function useAssistantContext(options) {
116
116
  var import_zustand = require("zustand");
117
117
 
118
118
  // src/types/ModelConfigTypes.ts
119
+ var import_zod = require("zod");
120
+ var LanguageModelV1CallSettingsSchema = import_zod.z.object({
121
+ maxTokens: import_zod.z.number().int().positive().optional(),
122
+ temperature: import_zod.z.number().optional(),
123
+ topP: import_zod.z.number().optional(),
124
+ presencePenalty: import_zod.z.number().optional(),
125
+ frequencyPenalty: import_zod.z.number().optional(),
126
+ seed: import_zod.z.number().int().optional(),
127
+ headers: import_zod.z.record(import_zod.z.string().optional()).optional()
128
+ });
129
+ var LanguageModelConfigSchema = import_zod.z.object({
130
+ apiKey: import_zod.z.string().optional(),
131
+ baseUrl: import_zod.z.string().optional(),
132
+ modelName: import_zod.z.string().optional()
133
+ });
119
134
  var mergeModelConfigs = (configSet) => {
120
135
  const configs = Array.from(configSet).map((c) => c.getModelConfig()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
121
136
  return configs.reduce((acc, config) => {
@@ -2283,6 +2298,10 @@ var toCoreMessages = (message) => {
2283
2298
  role: message2.role,
2284
2299
  content: message2.content.map((part) => {
2285
2300
  if (part.type === "ui") throw new Error("UI parts are not supported");
2301
+ if (part.type === "tool-call") {
2302
+ const { argsText, ...rest } = part;
2303
+ return rest;
2304
+ }
2286
2305
  return part;
2287
2306
  })
2288
2307
  };
@@ -2290,7 +2309,7 @@ var toCoreMessages = (message) => {
2290
2309
  };
2291
2310
 
2292
2311
  // src/runtimes/edge/converters/toLanguageModelTools.ts
2293
- var import_zod = require("zod");
2312
+ var import_zod2 = require("zod");
2294
2313
  var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
2295
2314
  var toLanguageModelTools = (tools) => {
2296
2315
  if (!tools) return [];
@@ -2298,7 +2317,7 @@ var toLanguageModelTools = (tools) => {
2298
2317
  type: "function",
2299
2318
  name,
2300
2319
  ...tool.description ? { description: tool.description } : void 0,
2301
- parameters: tool.parameters instanceof import_zod.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
2320
+ parameters: tool.parameters instanceof import_zod2.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
2302
2321
  }));
2303
2322
  };
2304
2323
 
@@ -2875,7 +2894,7 @@ var appendOrUpdateFinish = (message, chunk) => {
2875
2894
  };
2876
2895
 
2877
2896
  // src/runtimes/edge/streams/toolResultStream.ts
2878
- var import_zod2 = require("zod");
2897
+ var import_zod3 = require("zod");
2879
2898
  var import_secure_json_parse2 = __toESM(require("secure-json-parse"));
2880
2899
  function toolResultStream(tools) {
2881
2900
  const toolCallExecutions = /* @__PURE__ */ new Map();
@@ -2889,12 +2908,16 @@ function toolResultStream(tools) {
2889
2908
  const tool = tools?.[toolName];
2890
2909
  if (!tool || !tool.execute) return;
2891
2910
  const args = import_secure_json_parse2.default.parse(argsText);
2892
- if (tool.parameters instanceof import_zod2.z.ZodType) {
2911
+ if (tool.parameters instanceof import_zod3.z.ZodType) {
2893
2912
  const result = tool.parameters.safeParse(args);
2894
2913
  if (!result.success) {
2895
2914
  controller.enqueue({
2896
- type: "error",
2897
- error: new Error("Invalid tool call arguments")
2915
+ type: "tool-result",
2916
+ toolCallType,
2917
+ toolCallId,
2918
+ toolName,
2919
+ result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
2920
+ isError: true
2898
2921
  });
2899
2922
  return;
2900
2923
  } else {
@@ -2911,9 +2934,14 @@ function toolResultStream(tools) {
2911
2934
  result: result2
2912
2935
  });
2913
2936
  } catch (error) {
2937
+ console.error("Error: ", error);
2914
2938
  controller.enqueue({
2915
- type: "error",
2916
- error
2939
+ type: "tool-result",
2940
+ toolCallType,
2941
+ toolCallId,
2942
+ toolName,
2943
+ result: "Error: " + error,
2944
+ isError: true
2917
2945
  });
2918
2946
  } finally {
2919
2947
  toolCallExecutions.delete(toolCallId);
@@ -2971,7 +2999,9 @@ var EdgeChatAdapter = class {
2971
2999
  messages: toCoreMessages(messages),
2972
3000
  tools: toLanguageModelTools(
2973
3001
  config.tools
2974
- )
3002
+ ),
3003
+ ...config.callSettings,
3004
+ ...config.config
2975
3005
  }),
2976
3006
  signal: abortSignal
2977
3007
  });
@@ -3199,7 +3229,7 @@ var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
3199
3229
  toolCallId: part.toolCallId,
3200
3230
  toolName: part.toolName,
3201
3231
  argsText: JSON.stringify(part.args),
3202
- args: typeof part.args === "string" ? part.args : part.args
3232
+ args: part.args
3203
3233
  };
3204
3234
  }
3205
3235
  return part;
@@ -3339,47 +3369,47 @@ var LocalThreadRuntime = class {
3339
3369
  async startRun(parentId) {
3340
3370
  this.repository.resetHead(parentId);
3341
3371
  const messages = this.repository.getMessages();
3342
- const message = {
3372
+ let message = {
3343
3373
  id: generateId(),
3344
3374
  role: "assistant",
3345
3375
  status: { type: "in_progress" },
3346
3376
  content: [{ type: "text", text: "" }],
3347
3377
  createdAt: /* @__PURE__ */ new Date()
3348
3378
  };
3349
- this.repository.addOrUpdateMessage(parentId, { ...message });
3350
3379
  this.abortController?.abort();
3351
3380
  this.abortController = new AbortController();
3381
+ this.repository.addOrUpdateMessage(parentId, { ...message });
3352
3382
  this.notifySubscribers();
3353
- try {
3354
- const updateHandler = ({ content }) => {
3355
- message.content = content;
3356
- const newMessage = { ...message };
3357
- this.repository.addOrUpdateMessage(parentId, newMessage);
3358
- this.notifySubscribers();
3359
- return newMessage;
3383
+ const updateMessage = (m) => {
3384
+ message = {
3385
+ ...message,
3386
+ ...m
3360
3387
  };
3388
+ this.repository.addOrUpdateMessage(parentId, message);
3389
+ this.notifySubscribers();
3390
+ return message;
3391
+ };
3392
+ try {
3361
3393
  const result = await this.adapter.run({
3362
3394
  messages,
3363
3395
  abortSignal: this.abortController.signal,
3364
3396
  config: this.configProvider.getModelConfig(),
3365
- onUpdate: updateHandler
3397
+ onUpdate: updateMessage
3366
3398
  });
3367
- if (result !== void 0) {
3368
- updateHandler(result);
3369
- }
3370
3399
  if (result.status?.type === "in_progress")
3371
3400
  throw new Error(
3372
3401
  "Unexpected in_progress status returned from ChatModelAdapter"
3373
3402
  );
3374
- message.status = result.status ?? { type: "done" };
3403
+ this.abortController = null;
3404
+ updateMessage({ status: { type: "done" }, ...result });
3375
3405
  this.repository.addOrUpdateMessage(parentId, { ...message });
3376
3406
  } catch (e) {
3377
- message.status = { type: "error", error: e };
3378
- this.repository.addOrUpdateMessage(parentId, { ...message });
3379
- throw e;
3380
- } finally {
3407
+ const isAbortError = e instanceof Error && e.name === "AbortError";
3381
3408
  this.abortController = null;
3382
- this.notifySubscribers();
3409
+ updateMessage({
3410
+ status: isAbortError ? { type: "cancelled" } : { type: "error", error: e }
3411
+ });
3412
+ if (!isAbortError) throw e;
3383
3413
  }
3384
3414
  }
3385
3415
  cancelRun() {