@assistant-ui/react 0.10.44 → 0.10.45

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.
Files changed (53) hide show
  1. package/dist/model-context/frame/AssistantFrameHost.d.ts +37 -0
  2. package/dist/model-context/frame/AssistantFrameHost.d.ts.map +1 -0
  3. package/dist/model-context/frame/AssistantFrameHost.js +151 -0
  4. package/dist/model-context/frame/AssistantFrameHost.js.map +1 -0
  5. package/dist/model-context/frame/AssistantFrameProvider.d.ts +41 -0
  6. package/dist/model-context/frame/AssistantFrameProvider.d.ts.map +1 -0
  7. package/dist/model-context/frame/AssistantFrameProvider.js +142 -0
  8. package/dist/model-context/frame/AssistantFrameProvider.js.map +1 -0
  9. package/dist/model-context/frame/AssistantFrameTypes.d.ts +29 -0
  10. package/dist/model-context/frame/AssistantFrameTypes.d.ts.map +1 -0
  11. package/dist/model-context/frame/AssistantFrameTypes.js +6 -0
  12. package/dist/model-context/frame/AssistantFrameTypes.js.map +1 -0
  13. package/dist/model-context/frame/index.d.ts +5 -0
  14. package/dist/model-context/frame/index.d.ts.map +1 -0
  15. package/dist/model-context/frame/index.js +6 -0
  16. package/dist/model-context/frame/index.js.map +1 -0
  17. package/dist/model-context/frame/useAssistantFrameHost.d.ts +28 -0
  18. package/dist/model-context/frame/useAssistantFrameHost.d.ts.map +1 -0
  19. package/dist/model-context/frame/useAssistantFrameHost.js +25 -0
  20. package/dist/model-context/frame/useAssistantFrameHost.js.map +1 -0
  21. package/dist/model-context/index.d.ts +2 -0
  22. package/dist/model-context/index.d.ts.map +1 -1
  23. package/dist/model-context/index.js +2 -0
  24. package/dist/model-context/index.js.map +1 -1
  25. package/dist/model-context/registry/ModelContextRegistry.d.ts +19 -0
  26. package/dist/model-context/registry/ModelContextRegistry.d.ts.map +1 -0
  27. package/dist/model-context/registry/ModelContextRegistry.js +117 -0
  28. package/dist/model-context/registry/ModelContextRegistry.js.map +1 -0
  29. package/dist/model-context/registry/ModelContextRegistryHandles.d.ts +14 -0
  30. package/dist/model-context/registry/ModelContextRegistryHandles.d.ts.map +1 -0
  31. package/dist/model-context/registry/ModelContextRegistryHandles.js +1 -0
  32. package/dist/model-context/registry/ModelContextRegistryHandles.js.map +1 -0
  33. package/dist/model-context/registry/index.d.ts +3 -0
  34. package/dist/model-context/registry/index.d.ts.map +1 -0
  35. package/dist/model-context/registry/index.js +4 -0
  36. package/dist/model-context/registry/index.js.map +1 -0
  37. package/dist/model-context/useAssistantInstructions.d.ts +1 -2
  38. package/dist/model-context/useAssistantInstructions.d.ts.map +1 -1
  39. package/dist/model-context/useAssistantInstructions.js.map +1 -1
  40. package/package.json +3 -3
  41. package/src/model-context/frame/AssistantFrame.test.ts +353 -0
  42. package/src/model-context/frame/AssistantFrameHost.ts +218 -0
  43. package/src/model-context/frame/AssistantFrameProvider.ts +225 -0
  44. package/src/model-context/frame/AssistantFrameTypes.ts +40 -0
  45. package/src/model-context/frame/SPEC_AssistantFrame.md +104 -0
  46. package/src/model-context/frame/index.ts +4 -0
  47. package/src/model-context/frame/useAssistantFrameHost.ts +48 -0
  48. package/src/model-context/index.ts +3 -0
  49. package/src/model-context/registry/ModelContextRegistry.ts +165 -0
  50. package/src/model-context/registry/ModelContextRegistryHandles.ts +19 -0
  51. package/src/model-context/registry/SPEC_ModelContextRegistry.md +40 -0
  52. package/src/model-context/registry/index.ts +2 -0
  53. package/src/model-context/useAssistantInstructions.tsx +1 -1
@@ -0,0 +1,165 @@
1
+ import { Tool } from "assistant-stream";
2
+ import {
3
+ ModelContext,
4
+ ModelContextProvider,
5
+ mergeModelContexts,
6
+ } from "../../model-context/ModelContextTypes";
7
+ import { Unsubscribe } from "../../types/Unsubscribe";
8
+ import {
9
+ ModelContextRegistryToolHandle,
10
+ ModelContextRegistryInstructionHandle,
11
+ ModelContextRegistryProviderHandle,
12
+ } from "./ModelContextRegistryHandles";
13
+ import type { AssistantToolProps } from "../../model-context/useAssistantTool";
14
+ import type { AssistantInstructionsConfig } from "../../model-context/useAssistantInstructions";
15
+
16
+ export class ModelContextRegistry implements ModelContextProvider {
17
+ private _tools = new Map<symbol, AssistantToolProps<any, any>>();
18
+ private _instructions = new Map<symbol, string>();
19
+ private _providers = new Map<symbol, ModelContextProvider>();
20
+ private _subscribers = new Set<() => void>();
21
+ private _providerUnsubscribes = new Map<symbol, Unsubscribe | undefined>();
22
+
23
+ getModelContext(): ModelContext {
24
+ // Merge instructions
25
+ const instructions = Array.from(this._instructions.values()).filter(
26
+ Boolean,
27
+ );
28
+
29
+ const system =
30
+ instructions.length > 0 ? instructions.join("\n\n") : undefined;
31
+
32
+ // Collect tools
33
+ const tools: Record<string, Tool<any, any>> = {};
34
+ for (const toolProps of this._tools.values()) {
35
+ const { toolName, render, ...tool } = toolProps;
36
+ tools[toolName] = tool;
37
+ }
38
+
39
+ // Merge provider contexts
40
+ const providerContexts = mergeModelContexts(
41
+ new Set(this._providers.values()),
42
+ );
43
+
44
+ // Combine everything
45
+ const context: ModelContext = {
46
+ system,
47
+ tools: Object.keys(tools).length > 0 ? tools : undefined,
48
+ };
49
+
50
+ // Merge with provider contexts
51
+ if (providerContexts.system) {
52
+ context.system = context.system
53
+ ? `${context.system}\n\n${providerContexts.system}`
54
+ : providerContexts.system;
55
+ }
56
+
57
+ if (providerContexts.tools) {
58
+ context.tools = { ...(context.tools || {}), ...providerContexts.tools };
59
+ }
60
+
61
+ if (providerContexts.callSettings) {
62
+ context.callSettings = providerContexts.callSettings;
63
+ }
64
+
65
+ if (providerContexts.config) {
66
+ context.config = providerContexts.config;
67
+ }
68
+
69
+ return context;
70
+ }
71
+
72
+ subscribe(callback: () => void): Unsubscribe {
73
+ this._subscribers.add(callback);
74
+ return () => this._subscribers.delete(callback);
75
+ }
76
+
77
+ private notifySubscribers(): void {
78
+ for (const callback of this._subscribers) {
79
+ callback();
80
+ }
81
+ }
82
+
83
+ addTool<TArgs extends Record<string, unknown>, TResult>(
84
+ tool: AssistantToolProps<TArgs, TResult>,
85
+ ): ModelContextRegistryToolHandle<TArgs, TResult> {
86
+ const id = Symbol();
87
+
88
+ this._tools.set(id, tool);
89
+ this.notifySubscribers();
90
+
91
+ return {
92
+ update: (newTool: AssistantToolProps<TArgs, TResult>) => {
93
+ if (this._tools.has(id)) {
94
+ this._tools.set(id, newTool);
95
+ this.notifySubscribers();
96
+ }
97
+ },
98
+ remove: () => {
99
+ this._tools.delete(id);
100
+ this.notifySubscribers();
101
+ },
102
+ };
103
+ }
104
+
105
+ addInstruction(
106
+ config: string | AssistantInstructionsConfig,
107
+ ): ModelContextRegistryInstructionHandle {
108
+ const id = Symbol();
109
+
110
+ const instruction =
111
+ typeof config === "string" ? config : config.instruction;
112
+ const disabled = typeof config === "object" ? config.disabled : false;
113
+
114
+ if (!disabled) {
115
+ this._instructions.set(id, instruction);
116
+ this.notifySubscribers();
117
+ }
118
+
119
+ return {
120
+ update: (newConfig: string | AssistantInstructionsConfig) => {
121
+ const newInstruction =
122
+ typeof newConfig === "string" ? newConfig : newConfig.instruction;
123
+ const newDisabled =
124
+ typeof newConfig === "object" ? newConfig.disabled : false;
125
+
126
+ if (newDisabled) {
127
+ this._instructions.delete(id);
128
+ } else {
129
+ this._instructions.set(id, newInstruction);
130
+ }
131
+ this.notifySubscribers();
132
+ },
133
+ remove: () => {
134
+ this._instructions.delete(id);
135
+ this.notifySubscribers();
136
+ },
137
+ };
138
+ }
139
+
140
+ addProvider(
141
+ provider: ModelContextProvider,
142
+ ): ModelContextRegistryProviderHandle {
143
+ const id = Symbol();
144
+
145
+ this._providers.set(id, provider);
146
+
147
+ // Subscribe to provider changes
148
+ const unsubscribe = provider.subscribe?.(() => {
149
+ this.notifySubscribers();
150
+ });
151
+ this._providerUnsubscribes.set(id, unsubscribe);
152
+
153
+ this.notifySubscribers();
154
+
155
+ return {
156
+ remove: () => {
157
+ this._providers.delete(id);
158
+ const unsubscribe = this._providerUnsubscribes.get(id);
159
+ unsubscribe?.();
160
+ this._providerUnsubscribes.delete(id);
161
+ this.notifySubscribers();
162
+ },
163
+ };
164
+ }
165
+ }
@@ -0,0 +1,19 @@
1
+ import type { AssistantToolProps } from "../../model-context/useAssistantTool";
2
+ import type { AssistantInstructionsConfig } from "../../model-context/useAssistantInstructions";
3
+
4
+ export interface ModelContextRegistryToolHandle<
5
+ TArgs extends Record<string, unknown> = any,
6
+ TResult = any,
7
+ > {
8
+ update(tool: AssistantToolProps<TArgs, TResult>): void;
9
+ remove(): void;
10
+ }
11
+
12
+ export interface ModelContextRegistryInstructionHandle {
13
+ update(config: string | AssistantInstructionsConfig): void;
14
+ remove(): void;
15
+ }
16
+
17
+ export interface ModelContextRegistryProviderHandle {
18
+ remove(): void;
19
+ }
@@ -0,0 +1,40 @@
1
+ # ModelContextRegistry
2
+
3
+ An imperative API for registering tools and instructions to a model context provider.
4
+
5
+ ```typescript
6
+ const registry = new ModelContextRegistry();
7
+
8
+ const handle = registry.addTool({
9
+ toolName: "search",
10
+ description: "Search the web",
11
+ parameters: z.object({ query: z.string() }),
12
+ execute: async (args) => {
13
+ return { results: ["..."] };
14
+ },
15
+ });
16
+ ```
17
+
18
+ ## API
19
+
20
+ - addTool(tool: Tool & { toolName: string }): ModelContextRegistryToolHandle
21
+ - addInstruction(instruction: string): ModelContextRegistryInstructionHandle
22
+ - addProvider(provider: ModelContextProvider): ModelContextRegistryProviderHandle
23
+
24
+ ## ModelContextRegistryToolHandle
25
+
26
+ - update(tool: Tool & { toolName: string }): void;
27
+ - remove(): void;
28
+
29
+ ## ModelContextRegistryInstructionHandle
30
+
31
+ - update(instruction: string): void;
32
+ - remove(): void;
33
+
34
+ ## ModelContextRegistryProviderHandle
35
+
36
+ - remove(): void;
37
+
38
+ ## ModelContextProvider
39
+
40
+ The registry is a ModelContextProvider.
@@ -0,0 +1,2 @@
1
+ export * from "./ModelContextRegistry";
2
+ export * from "./ModelContextRegistryHandles";
@@ -3,7 +3,7 @@
3
3
  import { useEffect } from "react";
4
4
  import { useAssistantRuntime } from "../context";
5
5
 
6
- type AssistantInstructionsConfig = {
6
+ export type AssistantInstructionsConfig = {
7
7
  disabled?: boolean | undefined;
8
8
  instruction: string;
9
9
  };