@soverius-ai/browser-models 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +23 -2
- package/package.json +85 -3
- package/src/agent.d.ts +52 -0
- package/src/agent.js +128 -0
- package/src/agent.js.map +1 -0
- package/src/copilotkit.d.ts +20 -0
- package/src/copilotkit.js +163 -0
- package/src/copilotkit.js.map +1 -0
- package/src/engines/opfs-cache.d.ts +11 -0
- package/src/engines/opfs-cache.js +43 -0
- package/src/engines/opfs-cache.js.map +1 -0
- package/src/engines/prompt-api.d.ts +50 -0
- package/src/engines/prompt-api.js +124 -0
- package/src/engines/prompt-api.js.map +1 -0
- package/src/engines/shared-load.d.ts +20 -0
- package/src/engines/shared-load.js +67 -0
- package/src/engines/shared-load.js.map +1 -0
- package/src/engines/transformers.d.ts +45 -0
- package/src/engines/transformers.js +209 -0
- package/src/engines/transformers.js.map +1 -0
- package/src/engines/web-llm-worker.d.ts +10 -0
- package/src/engines/web-llm-worker.js +15 -0
- package/src/engines/web-llm-worker.js.map +1 -0
- package/src/engines/web-llm.d.ts +41 -0
- package/src/engines/web-llm.js +135 -0
- package/src/engines/web-llm.js.map +1 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/lib/abort.d.ts +10 -0
- package/src/lib/abort.js +24 -0
- package/src/lib/abort.js.map +1 -0
- package/src/lib/async-queue.d.ts +8 -0
- package/src/lib/async-queue.js +35 -0
- package/src/lib/async-queue.js.map +1 -0
- package/src/lib/engine.d.ts +45 -0
- package/src/lib/engine.js +2 -0
- package/src/lib/engine.js.map +1 -0
- package/src/lib/parser.d.ts +40 -0
- package/src/lib/parser.js +124 -0
- package/src/lib/parser.js.map +1 -0
- package/src/lib/partial-json.d.ts +21 -0
- package/src/lib/partial-json.js +88 -0
- package/src/lib/partial-json.js.map +1 -0
- package/src/lib/prompt.d.ts +9 -0
- package/src/lib/prompt.js +82 -0
- package/src/lib/prompt.js.map +1 -0
- package/src/lib/repair.d.ts +47 -0
- package/src/lib/repair.js +217 -0
- package/src/lib/repair.js.map +1 -0
- package/src/lib/run.d.ts +40 -0
- package/src/lib/run.js +434 -0
- package/src/lib/run.js.map +1 -0
- package/src/lib/schema.d.ts +21 -0
- package/src/lib/schema.js +128 -0
- package/src/lib/schema.js.map +1 -0
- package/src/lib/tools.d.ts +33 -0
- package/src/lib/tools.js +20 -0
- package/src/lib/tools.js.map +1 -0
- package/src/mcp.d.ts +54 -0
- package/src/mcp.js +204 -0
- package/src/mcp.js.map +1 -0
- package/src/model-store.d.ts +72 -0
- package/src/model-store.js +139 -0
- package/src/model-store.js.map +1 -0
- package/src/prompt-api.d.ts +1 -0
- package/src/prompt-api.js +3 -0
- package/src/prompt-api.js.map +1 -0
- package/src/transformers.d.ts +1 -0
- package/src/transformers.js +3 -0
- package/src/transformers.js.map +1 -0
- package/src/web-llm-worker.d.ts +1 -0
- package/src/web-llm-worker.js +3 -0
- package/src/web-llm-worker.js.map +1 -0
- package/src/web-llm.d.ts +1 -0
- package/src/web-llm.js +3 -0
- package/src/web-llm.js.map +1 -0
- package/src/webmcp.d.ts +43 -0
- package/src/webmcp.js +58 -0
- package/src/webmcp.js.map +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Turns the AG-UI conversation into plain chat messages, with tool instructions in the system prompt. */
|
|
2
|
+
export function buildChatMessages(messages, context, tools) {
|
|
3
|
+
const system = [];
|
|
4
|
+
const chat = [];
|
|
5
|
+
const toolNames = new Map();
|
|
6
|
+
for (const message of messages) {
|
|
7
|
+
switch (message.role) {
|
|
8
|
+
case 'system':
|
|
9
|
+
case 'developer':
|
|
10
|
+
system.push(message.content);
|
|
11
|
+
break;
|
|
12
|
+
case 'user':
|
|
13
|
+
chat.push({ role: 'user', content: textOf(message.content) });
|
|
14
|
+
break;
|
|
15
|
+
case 'assistant': {
|
|
16
|
+
const calls = (message.toolCalls ?? []).map((call) => {
|
|
17
|
+
toolNames.set(call.id, call.function.name);
|
|
18
|
+
return toolCallBlock(call.function.name, call.function.arguments);
|
|
19
|
+
});
|
|
20
|
+
const content = [message.content ?? '', ...calls]
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.join('\n');
|
|
23
|
+
if (content)
|
|
24
|
+
chat.push({ role: 'assistant', content });
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case 'tool':
|
|
28
|
+
chat.push({
|
|
29
|
+
role: 'user',
|
|
30
|
+
content: toolResultBlock(toolNames.get(message.toolCallId) ?? 'unknown', message.error ?? (textOf(message.content) || EMPTY_RESULT)),
|
|
31
|
+
});
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
// Activity and reasoning messages are UI-only and not sent to the model.
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
for (const item of context)
|
|
39
|
+
system.push(`${item.description}:\n${item.value}`);
|
|
40
|
+
if (tools.length > 0)
|
|
41
|
+
system.push(toolInstructions(tools));
|
|
42
|
+
return system.length > 0
|
|
43
|
+
? [{ role: 'system', content: system.join('\n\n') }, ...chat]
|
|
44
|
+
: chat;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Said to the model when a tool returns nothing, for example a CopilotKit `registerComponent`
|
|
48
|
+
* tool that only shows a component. An empty result reads like a failure to small models.
|
|
49
|
+
*/
|
|
50
|
+
const EMPTY_RESULT = 'Done. The tool ran and returned no data.';
|
|
51
|
+
/** How a tool call looks in the conversation the model sees. */
|
|
52
|
+
export function toolCallBlock(name, argumentsJson) {
|
|
53
|
+
return ('```tool_call\n' +
|
|
54
|
+
`{"name": ${JSON.stringify(name)}, "arguments": ${argumentsJson || '{}'}}` +
|
|
55
|
+
'\n```');
|
|
56
|
+
}
|
|
57
|
+
/** How a tool result looks in the conversation the model sees. */
|
|
58
|
+
export function toolResultBlock(name, result) {
|
|
59
|
+
return '```tool_result\n' + JSON.stringify({ name, result }) + '\n```';
|
|
60
|
+
}
|
|
61
|
+
function toolInstructions(tools) {
|
|
62
|
+
const list = tools
|
|
63
|
+
.map((tool) => `- ${tool.name}: ${tool.description}\n arguments (JSON Schema): ${JSON.stringify(tool.parameters ?? { type: 'object' })}`)
|
|
64
|
+
.join('\n');
|
|
65
|
+
return [
|
|
66
|
+
'You can call tools. To call one, reply with a block like this and nothing after it:',
|
|
67
|
+
'```tool_call\n{"name": "<tool name>", "arguments": { ... }}\n```',
|
|
68
|
+
'Tool results come back in a ```tool_result block. Only call a tool when you need it.',
|
|
69
|
+
'After a tool result, answer the user in plain words. Never write a tool_result block yourself.',
|
|
70
|
+
'If a tool result is an error, do not give up: fix the arguments or call the tool that provides what is missing, then try again.',
|
|
71
|
+
'Available tools:',
|
|
72
|
+
list,
|
|
73
|
+
].join('\n');
|
|
74
|
+
}
|
|
75
|
+
function textOf(content) {
|
|
76
|
+
if (typeof content === 'string')
|
|
77
|
+
return content;
|
|
78
|
+
return content
|
|
79
|
+
.map((part) => (part.type === 'text' ? (part.text ?? '') : ''))
|
|
80
|
+
.join('');
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt.js","sourceRoot":"","sources":["../../../../../../libs/agent/browser-models/src/lib/prompt.ts"],"names":[],"mappings":"AAIA,0GAA0G;AAC1G,MAAM,UAAU,iBAAiB,CAC/B,QAA4B,EAC5B,OAA2B,EAC3B,KAA0B;IAE1B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,IAAI,GAAkB,EAAE,CAAC;IAC/B,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE5C,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;YACrB,KAAK,QAAQ,CAAC;YACd,KAAK,WAAW;gBACd,MAAM,CAAC,IAAI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,gGAAgG;QAChG,iIAAiI;QACjI,kBAAkB;QAClB,IAAI;KACL,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AAED,SAAS,MAAM,CACb,OAA4D;IAE5D,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,OAAO,CAAC;IAChD,OAAO,OAAO;SACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;SAC9D,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ToolCall, ToolSpec } from './tools.ts';
|
|
2
|
+
/** How a broken tool call was fixed, in the order the steps ran. */
|
|
3
|
+
export type RepairStep = 'json' | 'name' | 'coerce' | 'reask' | 'hook';
|
|
4
|
+
export type RepairResult = {
|
|
5
|
+
readonly ok: true;
|
|
6
|
+
readonly call: ToolCall;
|
|
7
|
+
readonly steps: readonly RepairStep[];
|
|
8
|
+
} | {
|
|
9
|
+
readonly ok: false;
|
|
10
|
+
readonly error: string;
|
|
11
|
+
};
|
|
12
|
+
export interface RepairOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Asks the model once more for valid arguments. Returns the new raw arguments
|
|
15
|
+
* text (or a whole call), or undefined when the engine cannot be asked again.
|
|
16
|
+
*/
|
|
17
|
+
readonly reask?: (tool: ToolSpec, error: string,
|
|
18
|
+
/** The raw call that had the error. */
|
|
19
|
+
raw: string) => Promise<string | undefined>;
|
|
20
|
+
/** Your own last-chance fix. Return a call to use it, or undefined to give up. */
|
|
21
|
+
readonly hook?: (input: {
|
|
22
|
+
readonly raw: string;
|
|
23
|
+
readonly error: string;
|
|
24
|
+
readonly tools: readonly ToolSpec[];
|
|
25
|
+
}) => Promise<ToolCall | undefined> | ToolCall | undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns the raw text of a tool call into a valid call for one of `tools`. Steps, stopping at
|
|
29
|
+
* the first success: JSON repair (jsonrepair) → tool name repair → schema coercion → re-ask
|
|
30
|
+
* the model → user hook.
|
|
31
|
+
*/
|
|
32
|
+
export declare class ToolCallRepair {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(tools: readonly ToolSpec[], options?: RepairOptions);
|
|
35
|
+
/** Repairs one complete call. */
|
|
36
|
+
repair(raw: string): Promise<RepairResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Reads a call the model is still writing, so it can be shown while it streams. Returns
|
|
39
|
+
* nothing until the name is complete and (after name repair) a known tool. `argumentsJson`
|
|
40
|
+
* is the start of the arguments JSON that is already certain. It is a preview only: the
|
|
41
|
+
* complete call still goes through `repair`.
|
|
42
|
+
*/
|
|
43
|
+
readPartial(raw: string): {
|
|
44
|
+
readonly name: string;
|
|
45
|
+
readonly argumentsJson: string;
|
|
46
|
+
} | undefined;
|
|
47
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { jsonrepair } from 'jsonrepair';
|
|
2
|
+
import { closeJson } from "./partial-json.js";
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { ArgumentsSchema } from "./schema.js";
|
|
5
|
+
const jsonObject = z.record(z.string(), z.unknown());
|
|
6
|
+
/** A tool call as models write it. Some say `parameters` for `arguments`. */
|
|
7
|
+
const callEnvelope = z.looseObject({
|
|
8
|
+
name: z.string(),
|
|
9
|
+
arguments: z.unknown().optional(),
|
|
10
|
+
parameters: z.unknown().optional(),
|
|
11
|
+
});
|
|
12
|
+
/** For tools without parameters. One object, so its compiled schema is reused. */
|
|
13
|
+
const anyObject = { type: 'object' };
|
|
14
|
+
/**
|
|
15
|
+
* Turns the raw text of a tool call into a valid call for one of `tools`. Steps, stopping at
|
|
16
|
+
* the first success: JSON repair (jsonrepair) → tool name repair → schema coercion → re-ask
|
|
17
|
+
* the model → user hook.
|
|
18
|
+
*/
|
|
19
|
+
export class ToolCallRepair {
|
|
20
|
+
#tools;
|
|
21
|
+
#options;
|
|
22
|
+
constructor(tools, options = {}) {
|
|
23
|
+
this.#tools = tools;
|
|
24
|
+
this.#options = options;
|
|
25
|
+
}
|
|
26
|
+
/** Repairs one complete call. */
|
|
27
|
+
async repair(raw) {
|
|
28
|
+
const result = await this.#tryRepair(raw);
|
|
29
|
+
const hook = this.#options.hook;
|
|
30
|
+
if (result.ok || !hook)
|
|
31
|
+
return result;
|
|
32
|
+
// A failing hook must not end the run: the model is told the error instead.
|
|
33
|
+
let fixed;
|
|
34
|
+
try {
|
|
35
|
+
fixed = await hook({ raw, error: result.error, tools: this.#tools });
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
// Earlier steps did not lead to this call, so only the hook is reported.
|
|
41
|
+
return fixed ? { ok: true, call: fixed, steps: ['hook'] } : result;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads a call the model is still writing, so it can be shown while it streams. Returns
|
|
45
|
+
* nothing until the name is complete and (after name repair) a known tool. `argumentsJson`
|
|
46
|
+
* is the start of the arguments JSON that is already certain. It is a preview only: the
|
|
47
|
+
* complete call still goes through `repair`.
|
|
48
|
+
*/
|
|
49
|
+
readPartial(raw) {
|
|
50
|
+
// Well-formed JSON is closed exactly. JSON that needs repair (single quotes, bare keys) is
|
|
51
|
+
// read with jsonrepair, which has to guess: the arguments are then only a likely preview.
|
|
52
|
+
const closed = closeJson(raw);
|
|
53
|
+
const exact = parse(closed.json);
|
|
54
|
+
const parsed = exact ?? parse(() => jsonrepair(raw));
|
|
55
|
+
const call = callEnvelope.safeParse(parsed).data;
|
|
56
|
+
if (!call)
|
|
57
|
+
return undefined;
|
|
58
|
+
// The model is still writing the name itself.
|
|
59
|
+
const lastKey = Object.keys(jsonObject.safeParse(parsed).data ?? {}).at(-1);
|
|
60
|
+
if (lastKey === 'name' && (exact === undefined || closed.inString))
|
|
61
|
+
return undefined;
|
|
62
|
+
const tool = findTool(call.name, this.#tools);
|
|
63
|
+
if (!tool)
|
|
64
|
+
return undefined;
|
|
65
|
+
const args = jsonObject.safeParse(call.arguments ?? call.parameters);
|
|
66
|
+
if (!args.success)
|
|
67
|
+
return { name: tool.name, argumentsJson: '' };
|
|
68
|
+
const json = JSON.stringify(args.data);
|
|
69
|
+
if (exact === undefined)
|
|
70
|
+
return { name: tool.name, argumentsJson: certainStart(json, raw) };
|
|
71
|
+
// What is still open gets closed later: leave the closing characters out.
|
|
72
|
+
const start = json.replace(/[\]}]+$/, '');
|
|
73
|
+
return {
|
|
74
|
+
name: tool.name,
|
|
75
|
+
argumentsJson: closed.inString ? start.slice(0, -1) : start,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async #tryRepair(raw) {
|
|
79
|
+
const steps = [];
|
|
80
|
+
const call = callEnvelope.safeParse(parseJson(raw, steps)).data;
|
|
81
|
+
if (!call) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
error: 'The tool call must be a JSON object with a "name".',
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const tool = findTool(call.name, this.#tools);
|
|
88
|
+
if (!tool) {
|
|
89
|
+
const names = this.#tools.map((t) => t.name).join(', ');
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
error: `Unknown tool "${call.name}". Available tools: ${names}.`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (tool.name !== call.name)
|
|
96
|
+
steps.push('name');
|
|
97
|
+
let args = parseArguments(call.arguments ?? call.parameters ?? {}, steps);
|
|
98
|
+
let check = checkArguments(args, tool);
|
|
99
|
+
if (check.errors.length > 0 && this.#options.reask) {
|
|
100
|
+
// A failing re-ask (for example an engine that rejects the schema as a constraint)
|
|
101
|
+
// must not end the run: the model is told the error instead.
|
|
102
|
+
let answer;
|
|
103
|
+
try {
|
|
104
|
+
answer = await this.#options.reask(tool, check.errors.join('; '), raw);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
answer = undefined;
|
|
108
|
+
}
|
|
109
|
+
if (answer !== undefined) {
|
|
110
|
+
args = unwrapCall(parseArguments(answer, steps));
|
|
111
|
+
check = checkArguments(args, tool);
|
|
112
|
+
steps.push('reask');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (check.errors.length > 0) {
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
error: `Invalid arguments for "${tool.name}": ${check.errors.join('; ')}.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (check.coerced)
|
|
122
|
+
steps.push('coerce');
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
call: { name: tool.name, arguments: check.value },
|
|
126
|
+
steps,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The start of `json` (the arguments jsonrepair made of the unfinished `raw`) that stays the
|
|
132
|
+
* same when the rest of the call arrives. jsonrepair closes what is open and guesses the
|
|
133
|
+
* unfinished end: half a key becomes `"key":null`, half a `true` becomes the string "tr".
|
|
134
|
+
*/
|
|
135
|
+
function certainStart(json, raw) {
|
|
136
|
+
const open = json.replace(/[\]}]+$/, '');
|
|
137
|
+
const quotes = raw.replace(/\\./g, '').split('"').length - 1;
|
|
138
|
+
if (open.endsWith('"')) {
|
|
139
|
+
// Inside a string value: all of it but the closing quote is certain.
|
|
140
|
+
if (quotes % 2 === 1)
|
|
141
|
+
return open.slice(0, -1);
|
|
142
|
+
// A finished string, unless the raw text ends in a bare word such as `tr`.
|
|
143
|
+
if (!/\w$/.test(raw))
|
|
144
|
+
return open;
|
|
145
|
+
}
|
|
146
|
+
// A number, literal or key that may still grow: drop the whole last entry.
|
|
147
|
+
return open.slice(0, Math.max(open.lastIndexOf(','), open.lastIndexOf('{') + 1, open.lastIndexOf('[') + 1));
|
|
148
|
+
}
|
|
149
|
+
/** The parsed JSON, or undefined when it is not valid JSON. */
|
|
150
|
+
function parse(json) {
|
|
151
|
+
try {
|
|
152
|
+
return JSON.parse(typeof json === 'string' ? json : json());
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function checkArguments(args, tool) {
|
|
159
|
+
const check = ArgumentsSchema.of(tool.parameters ?? anyObject).check(args);
|
|
160
|
+
return { ...check, value: jsonObject.safeParse(check.value).data ?? {} };
|
|
161
|
+
}
|
|
162
|
+
function parseJson(text, steps) {
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(text);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
try {
|
|
168
|
+
const value = JSON.parse(jsonrepair(text));
|
|
169
|
+
steps.push('json');
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** A re-ask may be answered with a whole call (`{name, arguments}`) instead of the arguments. */
|
|
178
|
+
function unwrapCall(value) {
|
|
179
|
+
const call = callEnvelope.safeParse(value).data;
|
|
180
|
+
return call ? (call.arguments ?? call.parameters ?? value) : value;
|
|
181
|
+
}
|
|
182
|
+
function parseArguments(value, steps) {
|
|
183
|
+
// Some models send the arguments object as a JSON string.
|
|
184
|
+
return typeof value === 'string' ? parseJson(value, steps) : value;
|
|
185
|
+
}
|
|
186
|
+
/** Exact name first, then the same name in other spelling, then one unambiguous typo. */
|
|
187
|
+
function findTool(name, tools) {
|
|
188
|
+
const exact = tools.find((tool) => tool.name === name);
|
|
189
|
+
if (exact)
|
|
190
|
+
return exact;
|
|
191
|
+
const wanted = normalize(name);
|
|
192
|
+
const sameName = tools.filter((tool) => normalize(tool.name) === wanted);
|
|
193
|
+
if (sameName.length === 1)
|
|
194
|
+
return sameName[0];
|
|
195
|
+
const close = tools.filter((tool) => editDistance(normalize(tool.name), wanted) <= 2);
|
|
196
|
+
return close.length === 1 ? close[0] : undefined;
|
|
197
|
+
}
|
|
198
|
+
/** "functions.getWeather", "get-weather" and "GET_WEATHER" all become "getweather". */
|
|
199
|
+
function normalize(name) {
|
|
200
|
+
return name
|
|
201
|
+
.replace(/^functions\./, '')
|
|
202
|
+
.toLowerCase()
|
|
203
|
+
.replace(/[^a-z0-9]/g, '');
|
|
204
|
+
}
|
|
205
|
+
function editDistance(a, b) {
|
|
206
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
207
|
+
for (let i = 1; i <= a.length; i++) {
|
|
208
|
+
const current = [i];
|
|
209
|
+
for (let j = 1; j <= b.length; j++) {
|
|
210
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
211
|
+
current[j] = Math.min((previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, (previous[j - 1] ?? 0) + cost);
|
|
212
|
+
}
|
|
213
|
+
previous = current;
|
|
214
|
+
}
|
|
215
|
+
return previous[b.length] ?? 0;
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=repair.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repair.js","sourceRoot":"","sources":["../../../../../../libs/agent/browser-models/src/lib/repair.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAiC9C,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;AACrD,6EAA6E;AAC7E,MAAM,YAAY,GAAG,CAAC,CAAC,WAAW,CAAC;IACjC,IAAI,EAAE,CAAC,CAAC,MAA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AAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3C,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CACnB,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EACtB,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EACzB,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAC9B,CAAC;QACJ,CAAC;QACD,QAAQ,GAAG,OAAO,CAAC;IACrB,CAAC;IACD,OAAO,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC"}
|
package/src/lib/run.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Event as AgUiEvent, type RunAgentInput } from '@ag-ui/core';
|
|
2
|
+
import type { Engine } from './engine.ts';
|
|
3
|
+
import { type RepairOptions } from './repair.ts';
|
|
4
|
+
import { type LocalTool, type LocalToolSource } from './tools.ts';
|
|
5
|
+
export interface RunOptions {
|
|
6
|
+
readonly engine: Engine;
|
|
7
|
+
readonly input: RunAgentInput;
|
|
8
|
+
/**
|
|
9
|
+
* Tools that run inside the agent, or sources of them (read at the start of the run).
|
|
10
|
+
* Tools in `input.tools` are frontend tools, run by the client.
|
|
11
|
+
*/
|
|
12
|
+
readonly localTools?: readonly (LocalTool | LocalToolSource)[];
|
|
13
|
+
/** Most model calls in one run, counting re-tries after tool results. Default 5. */
|
|
14
|
+
readonly maxSteps?: number;
|
|
15
|
+
/** Optional last-chance repair hook. */
|
|
16
|
+
readonly repairHook?: RepairOptions['hook'];
|
|
17
|
+
readonly signal?: AbortSignal;
|
|
18
|
+
}
|
|
19
|
+
/** `activityType` of the model loading events. */
|
|
20
|
+
export declare const MODEL_ACTIVITY = "soverius.model";
|
|
21
|
+
/**
|
|
22
|
+
* One AG-UI run against a browser engine. `events()` yields its AG-UI events.
|
|
23
|
+
*
|
|
24
|
+
* - Text is streamed as TEXT_MESSAGE_* events.
|
|
25
|
+
* - A tool call is streamed while the model writes it: TOOL_CALL_START when the name is
|
|
26
|
+
* known, then TOOL_CALL_ARGS deltas.
|
|
27
|
+
* - Every tool call is repaired once it is complete (see ToolCallRepair). AG-UI cannot take
|
|
28
|
+
* back arguments that were sent, so the call stays open until it is repaired. When the
|
|
29
|
+
* repaired call continues what was sent, the rest follows as a delta. If not, a
|
|
30
|
+
* MESSAGES_SNAPSHOT replaces the call (or removes it, when it cannot be repaired). Only then
|
|
31
|
+
* comes TOOL_CALL_END, so at TOOL_CALL_END the client always holds the repaired call.
|
|
32
|
+
* - Local tools run here and the loop continues with their result.
|
|
33
|
+
* - A frontend tool call ends the run: the client runs it and starts a follow-up run.
|
|
34
|
+
*/
|
|
35
|
+
export declare class AgentRun {
|
|
36
|
+
#private;
|
|
37
|
+
constructor(options: RunOptions);
|
|
38
|
+
/** Runs the run. Call it once. */
|
|
39
|
+
events(): AsyncGenerator<AgUiEvent>;
|
|
40
|
+
}
|