genaicode 2.32.0 → 2.34.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/README.md +2 -0
- package/dist/agents/drivers/anthropic-managed.d.ts +141 -0
- package/dist/agents/drivers/anthropic-managed.js +753 -0
- package/dist/agents/drivers/anthropic-managed.js.map +1 -0
- package/dist/agents/drivers/cursor-cloud.d.ts +78 -0
- package/dist/agents/drivers/cursor-cloud.js +478 -0
- package/dist/agents/drivers/cursor-cloud.js.map +1 -0
- package/dist/agents/drivers/gemini-managed.d.ts +78 -0
- package/dist/agents/drivers/gemini-managed.js +526 -0
- package/dist/agents/drivers/gemini-managed.js.map +1 -0
- package/dist/agents/drivers/managed-http.d.ts +28 -0
- package/dist/agents/drivers/managed-http.js +142 -0
- package/dist/agents/drivers/managed-http.js.map +1 -0
- package/dist/agents/drivers/openai-managed.d.ts +79 -0
- package/dist/agents/drivers/openai-managed.js +402 -0
- package/dist/agents/drivers/openai-managed.js.map +1 -0
- package/dist/agents/failover.d.ts +19 -1
- package/dist/agents/failover.js +63 -2
- package/dist/agents/failover.js.map +1 -1
- package/dist/agents/hosted.d.ts +5 -1
- package/dist/agents/hosted.js +45 -132
- package/dist/agents/hosted.js.map +1 -1
- package/dist/agents/managed.d.ts +154 -0
- package/dist/agents/managed.js +561 -0
- package/dist/agents/managed.js.map +1 -0
- package/dist/agents/runtime.d.ts +5 -1
- package/dist/agents/runtime.js +9 -0
- package/dist/agents/runtime.js.map +1 -1
- package/dist/agents/session.d.ts +11 -0
- package/dist/agents/session.js.map +1 -1
- package/dist/agents.d.ts +11 -1
- package/dist/agents.js +6 -1
- package/dist/agents.js.map +1 -1
- package/dist/backend/backend.d.ts +131 -0
- package/dist/backend/backend.js +773 -0
- package/dist/backend/backend.js.map +1 -0
- package/dist/backend/limiter.d.ts +34 -0
- package/dist/backend/limiter.js +76 -0
- package/dist/backend/limiter.js.map +1 -0
- package/dist/backend/node.d.ts +10 -0
- package/dist/backend/node.js +124 -0
- package/dist/backend/node.js.map +1 -0
- package/dist/backend/protocol.d.ts +98 -0
- package/dist/backend/protocol.js +2 -0
- package/dist/backend/protocol.js.map +1 -0
- package/dist/backend/seal.d.ts +7 -0
- package/dist/backend/seal.js +60 -0
- package/dist/backend/seal.js.map +1 -0
- package/dist/backend.d.ts +27 -0
- package/dist/backend.js +25 -0
- package/dist/backend.js.map +1 -0
- package/dist/browser/fake-model.d.ts +43 -0
- package/dist/browser/fake-model.js +133 -0
- package/dist/browser/fake-model.js.map +1 -0
- package/dist/browser/invoke.d.ts +17 -0
- package/dist/browser/invoke.js +69 -0
- package/dist/browser/invoke.js.map +1 -0
- package/dist/browser/language-model.d.ts +74 -0
- package/dist/browser/language-model.js +83 -0
- package/dist/browser/language-model.js.map +1 -0
- package/dist/browser/nano.d.ts +93 -0
- package/dist/browser/nano.js +564 -0
- package/dist/browser/nano.js.map +1 -0
- package/dist/browser/page-agent.d.ts +81 -0
- package/dist/browser/page-agent.js +360 -0
- package/dist/browser/page-agent.js.map +1 -0
- package/dist/browser/remote.d.ts +44 -0
- package/dist/browser/remote.js +372 -0
- package/dist/browser/remote.js.map +1 -0
- package/dist/browser/tools.d.ts +36 -0
- package/dist/browser/tools.js +190 -0
- package/dist/browser/tools.js.map +1 -0
- package/dist/browser/webmcp.d.ts +32 -0
- package/dist/browser/webmcp.js +150 -0
- package/dist/browser/webmcp.js.map +1 -0
- package/dist/browser.d.ts +46 -0
- package/dist/browser.js +38 -0
- package/dist/browser.js.map +1 -0
- package/dist/core/types.d.ts +5 -0
- package/dist/core/types.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/google-converter.js +3 -0
- package/dist/providers/google-converter.js.map +1 -1
- package/dist/providers/google.js +7 -10
- package/dist/providers/google.js.map +1 -1
- package/dist/react/agent-chat.css +141 -0
- package/dist/react/agent-chat.d.ts +12 -2
- package/dist/react/agent-chat.js +32 -4
- package/dist/react/agent-chat.js.map +1 -1
- package/dist/react/attachments.d.ts +41 -0
- package/dist/react/attachments.js +162 -0
- package/dist/react/attachments.js.map +1 -0
- package/dist/react/page-agent-chat.d.ts +38 -0
- package/dist/react/page-agent-chat.js +44 -0
- package/dist/react/page-agent-chat.js.map +1 -0
- package/dist/react.d.ts +6 -1
- package/dist/react.js +2 -0
- package/dist/react.js.map +1 -1
- package/dist/ui/web-client.js +154 -13
- package/docs/CHANGELOG.md +38 -395
- package/docs/agents.md +207 -1
- package/docs/backend.md +125 -0
- package/docs/browser.md +205 -0
- package/docs/changelog-2.26-and-older.md +441 -0
- package/docs/pivot.md +1 -1
- package/docs/semver.md +2 -0
- package/package.json +9 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A stand-in for the browser's `LanguageModel` in tests: replies come from `script` in order
|
|
3
|
+
* (the last one repeats), and every prompt is recorded in `calls`. Each prompt adds 10 tokens to
|
|
4
|
+
* the context in use, up to `keeps` (default: no limit), the way a full window drops old messages.
|
|
5
|
+
*/
|
|
6
|
+
export function fakeLanguageModel(script, options = {}) {
|
|
7
|
+
const calls = [];
|
|
8
|
+
const created = [];
|
|
9
|
+
let destroyed = 0;
|
|
10
|
+
let copies = 0;
|
|
11
|
+
let released = 0;
|
|
12
|
+
let at = 0;
|
|
13
|
+
const next = async (input, promptOptions) => {
|
|
14
|
+
const text = textOf(input);
|
|
15
|
+
promptOptions?.signal?.throwIfAborted();
|
|
16
|
+
const reply = script[Math.min(at++, script.length - 1)];
|
|
17
|
+
if (reply === undefined)
|
|
18
|
+
throw new Error('fakeLanguageModel: nothing scripted');
|
|
19
|
+
return typeof reply === 'function' ? reply(text, promptOptions) : reply;
|
|
20
|
+
};
|
|
21
|
+
const factory = {
|
|
22
|
+
availability: async (asked) => (asked?.expectedInputs ?? []).some((input) => input.type !== 'text' && !options.media?.includes(input.type))
|
|
23
|
+
? 'unavailable'
|
|
24
|
+
: (options.availability ?? 'available'),
|
|
25
|
+
async create(createOptions = {}) {
|
|
26
|
+
created.push(createOptions);
|
|
27
|
+
if (options.download) {
|
|
28
|
+
const target = new EventTarget();
|
|
29
|
+
createOptions.monitor?.(target);
|
|
30
|
+
for (const loaded of options.download) {
|
|
31
|
+
target.dispatchEvent(Object.assign(new Event('downloadprogress'), { loaded }));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const make = (copy, held, start = 0) => {
|
|
35
|
+
let constraint = held;
|
|
36
|
+
// A copy starts from its parent's context and counts on its own from there.
|
|
37
|
+
let used = start;
|
|
38
|
+
const grow = () => {
|
|
39
|
+
used = Math.min(used + 10, options.keeps ?? Infinity);
|
|
40
|
+
};
|
|
41
|
+
const record = (input, promptOptions, streaming) => {
|
|
42
|
+
calls.push({ input: textOf(input), media: mediaOf(input), options: promptOptions, streaming, copy });
|
|
43
|
+
};
|
|
44
|
+
const answer = async (input, promptOptions) => {
|
|
45
|
+
const asked = promptOptions?.responseConstraint && JSON.stringify(promptOptions.responseConstraint);
|
|
46
|
+
if (options.stickyConstraint && asked) {
|
|
47
|
+
if (constraint !== undefined && constraint !== asked) {
|
|
48
|
+
throw new DOMException('An unknown error occurred: kErrorUnknown', 'UnknownError');
|
|
49
|
+
}
|
|
50
|
+
constraint = asked;
|
|
51
|
+
}
|
|
52
|
+
const reply = await next(input, promptOptions);
|
|
53
|
+
grow();
|
|
54
|
+
return reply;
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
async prompt(input, promptOptions) {
|
|
58
|
+
record(input, promptOptions, false);
|
|
59
|
+
return answer(input, promptOptions);
|
|
60
|
+
},
|
|
61
|
+
promptStreaming(input, promptOptions) {
|
|
62
|
+
record(input, promptOptions, true);
|
|
63
|
+
return new ReadableStream({
|
|
64
|
+
async start(controller) {
|
|
65
|
+
try {
|
|
66
|
+
const reply = await answer(input, promptOptions);
|
|
67
|
+
// Two chunks, so deltas are joined.
|
|
68
|
+
const half = Math.ceil(reply.length / 2);
|
|
69
|
+
controller.enqueue(reply.slice(0, half));
|
|
70
|
+
if (reply.length > half)
|
|
71
|
+
controller.enqueue(reply.slice(half));
|
|
72
|
+
controller.close();
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
controller.error(error);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
...(options.noClone
|
|
81
|
+
? {}
|
|
82
|
+
: {
|
|
83
|
+
async clone() {
|
|
84
|
+
copies++;
|
|
85
|
+
return make(true, constraint, used);
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
destroy() {
|
|
89
|
+
if (copy)
|
|
90
|
+
released++;
|
|
91
|
+
else
|
|
92
|
+
destroyed++;
|
|
93
|
+
},
|
|
94
|
+
get contextUsage() {
|
|
95
|
+
return used;
|
|
96
|
+
},
|
|
97
|
+
contextWindow: options.window ?? 1000,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
const session = make(false, undefined, options.initial ?? 0);
|
|
101
|
+
return session;
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
factory,
|
|
106
|
+
calls,
|
|
107
|
+
created,
|
|
108
|
+
get destroyed() {
|
|
109
|
+
return destroyed;
|
|
110
|
+
},
|
|
111
|
+
/** Copies made with `clone()`, and how many of them were destroyed. */
|
|
112
|
+
get copies() {
|
|
113
|
+
return copies;
|
|
114
|
+
},
|
|
115
|
+
get released() {
|
|
116
|
+
return released;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function parts(input) {
|
|
121
|
+
if (typeof input === 'string')
|
|
122
|
+
return [{ type: 'text', value: input }];
|
|
123
|
+
return input.flatMap((message) => typeof message.content === 'string' ? [{ type: 'text', value: message.content }] : message.content);
|
|
124
|
+
}
|
|
125
|
+
function textOf(input) {
|
|
126
|
+
return parts(input)
|
|
127
|
+
.map((part) => (part.type === 'text' ? String(part.value) : `[${part.type}]`))
|
|
128
|
+
.join('\n');
|
|
129
|
+
}
|
|
130
|
+
function mediaOf(input) {
|
|
131
|
+
return parts(input).filter((part) => part.type !== 'text');
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=fake-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-model.js","sourceRoot":"","sources":["../../src/browser/fake-model.ts"],"names":[],"mappings":"AAwBA;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAmB,EACnB,UAgBI,EAAE;IAEN,MAAM,KAAK,GAAe,EAAE,CAAC;IAC7B,MAAM,OAAO,GAAiC,EAAE,CAAC;IACjD,IAAI,SAAS,GAAG,CAAC,CAAC;IAClB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,MAAM,IAAI,GAAG,KAAK,EAAE,KAAsC,EAAE,aAA0C,EAAE,EAAE;QACxG,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QAC3B,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QACxD,IAAI,KAAK,KAAK,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAChF,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAC1E,CAAC,CAAC;IACF,MAAM,OAAO,GAAyB;QACpC,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,CAC5B,CAAC,KAAK,EAAE,cAAc,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,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;QAAE,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACvE,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAC/B,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC5G,CAAC;AACJ,CAAC;AAED,SAAS,MAAM,CAAC,KAAsC;IACpD,OAAO,KAAK,CAAC,KAAK,CAAC;SAChB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SAC7E,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAS,OAAO,CAAC,KAAsC;IACrD,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;AAC7D,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AgentEvent, AgentTask, ApprovalRequest } from '../agents/types.js';
|
|
2
|
+
import { type PageTool } from './tools.js';
|
|
3
|
+
/** Check the arguments, ask the user about a `write` tool, then run it. */
|
|
4
|
+
export declare function invoke(tool: PageTool, args: Record<string, unknown>, id: string, task: AgentTask, emit: (event: AgentEvent) => void, signal: AbortSignal, options?: {
|
|
5
|
+
/** Ask first even if the tool is not marked `write` (another side says it is one). */
|
|
6
|
+
write?: boolean;
|
|
7
|
+
/** Called just before a `write` tool runs: from then on it may have changed something. */
|
|
8
|
+
onWrite?: () => void;
|
|
9
|
+
}): Promise<string>;
|
|
10
|
+
/**
|
|
11
|
+
* Ask the user about `request` as `task.permissions` says: `read-only` and `not-allowed` without
|
|
12
|
+
* asking (a read-only task, or approvals set to `deny`), otherwise the user's answer, reported as
|
|
13
|
+
* `approval-request` and `approval-resolved` events.
|
|
14
|
+
*/
|
|
15
|
+
export declare function ask(request: ApprovalRequest, task: AgentTask, emit: (event: AgentEvent) => void, signal: AbortSignal): Promise<'approve' | 'deny' | 'read-only' | 'not-allowed'>;
|
|
16
|
+
/** JSON with sorted keys: the same arguments always read the same. */
|
|
17
|
+
export declare function stableJson(value: Record<string, unknown>): string;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { resolvePermissions } from '../agents/permissions.js';
|
|
2
|
+
import { abortable } from './language-model.js';
|
|
3
|
+
import { argumentProblems, toolText } from './tools.js';
|
|
4
|
+
/** Check the arguments, ask the user about a `write` tool, then run it. */
|
|
5
|
+
export async function invoke(tool, args, id, task, emit, signal, options = {}) {
|
|
6
|
+
const problems = argumentProblems(tool, args);
|
|
7
|
+
if (problems.length)
|
|
8
|
+
throw new Error(`Bad arguments: ${problems.join('; ')}.`);
|
|
9
|
+
tool.validate?.(args);
|
|
10
|
+
if (tool.write || options.write) {
|
|
11
|
+
const request = {
|
|
12
|
+
id,
|
|
13
|
+
kind: 'other',
|
|
14
|
+
summary: tool.summary?.(args) ?? `${tool.name} ${stableJson(args)}`,
|
|
15
|
+
detail: { tool: tool.name, args },
|
|
16
|
+
scope: 'once',
|
|
17
|
+
};
|
|
18
|
+
const decision = await ask(request, task, emit, signal);
|
|
19
|
+
if (decision === 'read-only')
|
|
20
|
+
throw new Error('Not allowed: this task is read-only.');
|
|
21
|
+
if (decision === 'not-allowed')
|
|
22
|
+
throw new Error('Not allowed: this needs the user’s approval.');
|
|
23
|
+
if (decision !== 'approve')
|
|
24
|
+
throw new Error('The user declined this action.');
|
|
25
|
+
options.onWrite?.();
|
|
26
|
+
}
|
|
27
|
+
return toolText(await abortable(Promise.resolve(tool.run(args, signal)), signal));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Ask the user about `request` as `task.permissions` says: `read-only` and `not-allowed` without
|
|
31
|
+
* asking (a read-only task, or approvals set to `deny`), otherwise the user's answer, reported as
|
|
32
|
+
* `approval-request` and `approval-resolved` events.
|
|
33
|
+
*/
|
|
34
|
+
export async function ask(request, task, emit, signal) {
|
|
35
|
+
const permissions = resolvePermissions(task.permissions);
|
|
36
|
+
if (permissions.sandbox === 'read-only')
|
|
37
|
+
return 'read-only';
|
|
38
|
+
const approval = permissions.approval ?? 'ask';
|
|
39
|
+
if (approval === 'deny')
|
|
40
|
+
return 'not-allowed';
|
|
41
|
+
emit({ type: 'approval-request', request });
|
|
42
|
+
let decision = 'deny';
|
|
43
|
+
if (approval === 'auto-approve')
|
|
44
|
+
decision = 'approve';
|
|
45
|
+
else if (task.onApproval) {
|
|
46
|
+
try {
|
|
47
|
+
decision =
|
|
48
|
+
(await abortable(Promise.resolve(task.onApproval(request, signal)), signal)) === 'approve' ? 'approve' : 'deny';
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
decision = 'deny';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (signal.aborted)
|
|
55
|
+
decision = 'deny';
|
|
56
|
+
emit({
|
|
57
|
+
type: 'approval-resolved',
|
|
58
|
+
id: request.id,
|
|
59
|
+
decision,
|
|
60
|
+
...(approval === 'auto-approve' ? { automatic: true } : {}),
|
|
61
|
+
});
|
|
62
|
+
signal.throwIfAborted();
|
|
63
|
+
return decision;
|
|
64
|
+
}
|
|
65
|
+
/** JSON with sorted keys: the same arguments always read the same. */
|
|
66
|
+
export function stableJson(value) {
|
|
67
|
+
return JSON.stringify(Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b))));
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=invoke.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invoke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AQ,GAAG,MAAM,CAAC;IACtC,IAAI,CAAC;QACH,IAAI,EAAE,mBAAmB;QACzB,EAAE,EAAE,OAAO,CAAC,EAAE;QACd,QAAQ;QACR,GAAG,CAAC,QAAQ,KAAK,cAAc,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC5D,CAAC,CAAC;IACH,MAAM,CAAC,cAAc,EAAE,CAAC;IACxB,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,UAAU,CAAC,KAA8B;IACvD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The part of the browser's Prompt API (`LanguageModel`, Gemini Nano in Chrome 148+) that
|
|
3
|
+
* genaicode uses. Typed here because TypeScript's DOM library does not have it yet.
|
|
4
|
+
*/
|
|
5
|
+
export interface LanguageModelMessage {
|
|
6
|
+
role: 'system' | 'user' | 'assistant';
|
|
7
|
+
content: string | LanguageModelContent[];
|
|
8
|
+
}
|
|
9
|
+
/** What a model may take besides text. */
|
|
10
|
+
export type LanguageModelMedia = 'image' | 'audio';
|
|
11
|
+
/** One part of a message: text, an image (a `Blob`, a canvas, an `<img>`) or audio (a `Blob`, an `AudioBuffer`). */
|
|
12
|
+
export type LanguageModelContent = {
|
|
13
|
+
type: 'text';
|
|
14
|
+
value: string;
|
|
15
|
+
} | {
|
|
16
|
+
type: LanguageModelMedia;
|
|
17
|
+
value: unknown;
|
|
18
|
+
};
|
|
19
|
+
export interface LanguageModelPromptOptions {
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
/** A JSON Schema the reply must match. */
|
|
22
|
+
responseConstraint?: Record<string, unknown>;
|
|
23
|
+
/** The prompt already says what shape is wanted: do not spend context on the schema. */
|
|
24
|
+
omitResponseConstraintInput?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface LanguageModelSession {
|
|
27
|
+
prompt(input: string | LanguageModelMessage[], options?: LanguageModelPromptOptions): Promise<string>;
|
|
28
|
+
promptStreaming(input: string | LanguageModelMessage[], options?: LanguageModelPromptOptions): ReadableStream<string>;
|
|
29
|
+
/** A copy with the same conversation so far; what is asked of the copy stays out of this session. */
|
|
30
|
+
clone?(options?: {
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
}): Promise<LanguageModelSession>;
|
|
33
|
+
destroy(): void;
|
|
34
|
+
/** Tokens of the context window in use. `inputUsage` in Chrome before 148. */
|
|
35
|
+
readonly contextUsage?: number;
|
|
36
|
+
readonly contextWindow?: number;
|
|
37
|
+
readonly inputUsage?: number;
|
|
38
|
+
readonly inputQuota?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface LanguageModelExpected {
|
|
41
|
+
type: 'text' | LanguageModelMedia;
|
|
42
|
+
languages?: string[];
|
|
43
|
+
}
|
|
44
|
+
export interface LanguageModelCreateOptions {
|
|
45
|
+
initialPrompts?: LanguageModelMessage[];
|
|
46
|
+
expectedInputs?: LanguageModelExpected[];
|
|
47
|
+
expectedOutputs?: LanguageModelExpected[];
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
monitor?(monitor: EventTarget): void;
|
|
50
|
+
}
|
|
51
|
+
/** `downloadable` and `downloading`: usable after a download that `create()` starts. */
|
|
52
|
+
export type LanguageModelAvailability = 'unavailable' | 'downloadable' | 'downloading' | 'available';
|
|
53
|
+
export interface LanguageModelFactory {
|
|
54
|
+
availability(options?: Pick<LanguageModelCreateOptions, 'expectedInputs' | 'expectedOutputs'>): Promise<string>;
|
|
55
|
+
create(options?: LanguageModelCreateOptions): Promise<LanguageModelSession>;
|
|
56
|
+
}
|
|
57
|
+
/** The browser's `LanguageModel`, or undefined where the Prompt API does not exist. */
|
|
58
|
+
export declare function builtInLanguageModel(): LanguageModelFactory | undefined;
|
|
59
|
+
/** What `availability()` and `create()` are told to expect: text in one language both ways, and `media` in. */
|
|
60
|
+
export declare function expectedText(language: string, media?: readonly LanguageModelMedia[]): Pick<LanguageModelCreateOptions, 'expectedInputs' | 'expectedOutputs'>;
|
|
61
|
+
/**
|
|
62
|
+
* Whether the browser has a built-in model for `language` (default English): `unavailable`
|
|
63
|
+
* also when the Prompt API is missing or refuses to say.
|
|
64
|
+
*/
|
|
65
|
+
export declare function languageModelAvailability(language?: string, factory?: LanguageModelFactory | undefined, media?: readonly LanguageModelMedia[]): Promise<LanguageModelAvailability>;
|
|
66
|
+
/** Tokens in use and the size of a session's context window, where the browser reports them. */
|
|
67
|
+
export declare function contextOf(session: LanguageModelSession): {
|
|
68
|
+
used: number;
|
|
69
|
+
window: number;
|
|
70
|
+
} | undefined;
|
|
71
|
+
/** Read a `promptStreaming()` stream: each chunk is the next piece of text, as Chrome 148 sends it. */
|
|
72
|
+
export declare function streamDeltas(stream: ReadableStream<string>, signal?: AbortSignal): AsyncGenerator<string>;
|
|
73
|
+
/** `promise`, or a rejection as soon as `signal` aborts: a model that ignores the signal must not hold a task. */
|
|
74
|
+
export declare function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** The browser's `LanguageModel`, or undefined where the Prompt API does not exist. */
|
|
2
|
+
export function builtInLanguageModel() {
|
|
3
|
+
const candidate = globalThis.LanguageModel;
|
|
4
|
+
return candidate && typeof candidate.create === 'function' && typeof candidate.availability === 'function'
|
|
5
|
+
? candidate
|
|
6
|
+
: undefined;
|
|
7
|
+
}
|
|
8
|
+
/** What `availability()` and `create()` are told to expect: text in one language both ways, and `media` in. */
|
|
9
|
+
export function expectedText(language, media = []) {
|
|
10
|
+
return {
|
|
11
|
+
expectedInputs: [{ type: 'text', languages: [language] }, ...media.map((type) => ({ type }))],
|
|
12
|
+
expectedOutputs: [{ type: 'text', languages: [language] }],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether the browser has a built-in model for `language` (default English): `unavailable`
|
|
17
|
+
* also when the Prompt API is missing or refuses to say.
|
|
18
|
+
*/
|
|
19
|
+
export async function languageModelAvailability(language = 'en', factory = builtInLanguageModel(), media = []) {
|
|
20
|
+
if (!factory)
|
|
21
|
+
return 'unavailable';
|
|
22
|
+
try {
|
|
23
|
+
const answer = await factory.availability(expectedText(language, media));
|
|
24
|
+
// Chrome before 138 answered `readily` and `after-download`.
|
|
25
|
+
if (answer === 'available' || answer === 'readily')
|
|
26
|
+
return 'available';
|
|
27
|
+
if (answer === 'downloadable' || answer === 'after-download')
|
|
28
|
+
return 'downloadable';
|
|
29
|
+
if (answer === 'downloading')
|
|
30
|
+
return 'downloading';
|
|
31
|
+
return 'unavailable';
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return 'unavailable';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Tokens in use and the size of a session's context window, where the browser reports them. */
|
|
38
|
+
export function contextOf(session) {
|
|
39
|
+
const used = session.contextUsage ?? session.inputUsage;
|
|
40
|
+
const window = session.contextWindow ?? session.inputQuota;
|
|
41
|
+
return typeof used === 'number' && typeof window === 'number' && window > 0 ? { used, window } : undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Read a `promptStreaming()` stream: each chunk is the next piece of text, as Chrome 148 sends it. */
|
|
44
|
+
export async function* streamDeltas(stream, signal) {
|
|
45
|
+
const reader = stream.getReader();
|
|
46
|
+
try {
|
|
47
|
+
for (;;) {
|
|
48
|
+
if (signal?.aborted)
|
|
49
|
+
return;
|
|
50
|
+
const { done, value } = await abortable(reader.read(), signal);
|
|
51
|
+
if (done)
|
|
52
|
+
return;
|
|
53
|
+
if (typeof value === 'string' && value)
|
|
54
|
+
yield value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
// An abandoned stream keeps the model busy.
|
|
59
|
+
reader.cancel().catch(() => { });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** `promise`, or a rejection as soon as `signal` aborts: a model that ignores the signal must not hold a task. */
|
|
63
|
+
export function abortable(promise, signal) {
|
|
64
|
+
if (!signal)
|
|
65
|
+
return promise;
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const stop = () => reject(signal.reason instanceof Error ? signal.reason : new DOMException('Aborted', 'AbortError'));
|
|
68
|
+
if (signal.aborted) {
|
|
69
|
+
// Nobody waits for it any more; its own rejection must not go unhandled.
|
|
70
|
+
promise.catch(() => { });
|
|
71
|
+
return stop();
|
|
72
|
+
}
|
|
73
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
74
|
+
promise.then((value) => {
|
|
75
|
+
signal.removeEventListener('abort', stop);
|
|
76
|
+
resolve(value);
|
|
77
|
+
}, (error) => {
|
|
78
|
+
signal.removeEventListener('abort', stop);
|
|
79
|
+
reject(error);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=language-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"language-model.js","sourceRoot":"","sources":["../../src/browser/language-model.ts"],"names":[],"mappings":"AAyDA,uFAAuF;AACvF,MAAM,UAAU,oBAAoB;IAClC,MAAM,SAAS,GAAI,UAAgE,CAAC,aAAa,CAAC;IAClG,OAAO,SAAS,IAAI,OAAO,SAAS,CAAC,MAAM,KAAK,UAAU,IAAI,OAAO,SAAS,CAAC,YAAY,KAAK,UAAU;QACxG,CAAC,CAAE,SAAkC;QACrC,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,YAAY,CAC1B,QAAgB,EAChB,QAAuC,EAAE;IAE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AC;QACD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,OAAO,CAAC,IAAI,CACV,CAAC,KAAK,EAAE,EAAE;YACR,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1C,OAAO,CAAC,KAAK,CAAC,CAAC;QACjB,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;YACjB,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1C,MAAM,CAAC,KAAK,CAAC,CAAC;QAChB,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { AgentRun, AgentTask, CodingAgent } from '../agents/types.js';
|
|
2
|
+
import { type LanguageModelAvailability, type LanguageModelFactory, type LanguageModelMedia } from './language-model.js';
|
|
3
|
+
import { type PageTool } from './tools.js';
|
|
4
|
+
export interface NanoOptions {
|
|
5
|
+
/** What the model may do on the page. A function is called at the start of every task. */
|
|
6
|
+
tools?: readonly PageTool[] | (() => readonly PageTool[] | Promise<readonly PageTool[]>);
|
|
7
|
+
/** Who the assistant is and what the page is about. Keep it short: the context window is small. */
|
|
8
|
+
instructions?: string;
|
|
9
|
+
/** Language of the conversation (the Prompt API supports a few). Default `en`. */
|
|
10
|
+
language?: string;
|
|
11
|
+
/** Most tool calls in one task before the model has to answer. Default 6. */
|
|
12
|
+
maxSteps?: number;
|
|
13
|
+
/** Longest tool output the model is shown, in characters. Default 2,000. */
|
|
14
|
+
maxToolOutput?: number;
|
|
15
|
+
/** Shown as the model's name. Default `on-device`. */
|
|
16
|
+
model?: string;
|
|
17
|
+
/** Agent name. Default `nano`. */
|
|
18
|
+
name?: string;
|
|
19
|
+
/** The Prompt API to use. Default: the browser's `LanguageModel`. */
|
|
20
|
+
languageModel?: LanguageModelFactory;
|
|
21
|
+
/**
|
|
22
|
+
* What a task may attach besides text files: images, audio (a voice message). Asked of the
|
|
23
|
+
* browser's model, which may take neither; `inputs()` says what it does take. Default: none.
|
|
24
|
+
*/
|
|
25
|
+
inputs?: readonly LanguageModelMedia[];
|
|
26
|
+
/** Longest text of one attached file the model is shown, in characters. Default 4,000. */
|
|
27
|
+
maxAttachmentText?: number;
|
|
28
|
+
/** Download progress of the model, 0 to 1, while a task or `prepare()` waits for it. */
|
|
29
|
+
onDownload?(progress: number): void;
|
|
30
|
+
/** Called after every call to the model, with how long it took: for logs and tuning. */
|
|
31
|
+
onModelCall?(call: NanoModelCall): void;
|
|
32
|
+
}
|
|
33
|
+
/** One call to the model within a task. */
|
|
34
|
+
export interface NanoModelCall {
|
|
35
|
+
/** `choose`: tool or reply. `arguments`: the tool's arguments. `reply`: the answer. */
|
|
36
|
+
step: 'choose' | 'arguments' | 'reply';
|
|
37
|
+
ms: number;
|
|
38
|
+
/** Whether the browser held the answer to a JSON Schema. */
|
|
39
|
+
constrained: boolean;
|
|
40
|
+
/** Why the call failed. */
|
|
41
|
+
error?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A task for `nano()`: an `AgentTask` that may carry files. A text file is read into the prompt,
|
|
45
|
+
* an image or audio goes to the model as it is (`NanoOptions.inputs`). A `File` keeps its name.
|
|
46
|
+
*/
|
|
47
|
+
export interface NanoTask extends AgentTask {
|
|
48
|
+
attachments?: readonly Blob[];
|
|
49
|
+
}
|
|
50
|
+
/** A `CodingAgent` that runs in the page, on the browser's built-in model. */
|
|
51
|
+
export interface NanoAgent extends CodingAgent {
|
|
52
|
+
run(task: NanoTask): AgentRun;
|
|
53
|
+
/** Which of `NanoOptions.inputs` this browser's model takes. Empty where it takes text only. */
|
|
54
|
+
inputs(): Promise<LanguageModelMedia[]>;
|
|
55
|
+
/** Whether the browser has the model, could download it, or cannot run it at all. */
|
|
56
|
+
availability(): Promise<LanguageModelAvailability>;
|
|
57
|
+
/**
|
|
58
|
+
* Load the model now, downloading it first when needed (several GB, once per browser), so the
|
|
59
|
+
* first task does not wait. A download needs a user gesture: call this from a click.
|
|
60
|
+
*/
|
|
61
|
+
prepare(signal?: AbortSignal): Promise<void>;
|
|
62
|
+
/** Tokens in use and the size of a conversation's context window, where the browser says. */
|
|
63
|
+
context(sessionId: string): {
|
|
64
|
+
used: number;
|
|
65
|
+
window: number;
|
|
66
|
+
} | undefined;
|
|
67
|
+
/** Drop a conversation (`sessionId` of a result), or all of them, and free the model's memory. */
|
|
68
|
+
forget(sessionId?: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* Whether this browser can run the model (has it, or can download it), and what it takes
|
|
71
|
+
* besides text: what `withFallback` and `createPageAgent({ agent })` ask of an agent.
|
|
72
|
+
*/
|
|
73
|
+
info(): Promise<{
|
|
74
|
+
available: boolean;
|
|
75
|
+
inputs: LanguageModelMedia[];
|
|
76
|
+
}>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The browser's built-in model (Gemini Nano through Chrome's Prompt API) as a genaicode agent.
|
|
80
|
+
* It runs in the page, costs nothing and sends nothing to a server; it is a small model, good
|
|
81
|
+
* for one or two tool calls and a short answer, not for coding.
|
|
82
|
+
*
|
|
83
|
+
* The Prompt API has no tool calling, so each step is a constrained choice: the model picks a
|
|
84
|
+
* tool or `reply`, then fills in that tool's arguments against its JSON Schema, and the final
|
|
85
|
+
* answer streams as text. `write` tools go through `task.onApproval` first, and never run under
|
|
86
|
+
* `permissions.sandbox: 'read-only'`; other sandbox values fail the task, as a page has none. `task.resume`
|
|
87
|
+
* continues a conversation; `task.cwd`, `env` and `mcpServers` mean nothing here.
|
|
88
|
+
*/
|
|
89
|
+
export declare function nano(options?: NanoOptions): NanoAgent;
|
|
90
|
+
export declare const TEXT_TYPES: RegExp;
|
|
91
|
+
export declare const TEXT_NAMES: RegExp;
|
|
92
|
+
/** What the model is told once, as the system prompt. */
|
|
93
|
+
export declare function briefing(instructions: string | undefined, tools: readonly PageTool[]): string;
|