effect-assistant 0.15.0 → 0.17.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/dist/StatusSummary.d.ts +4 -3
- package/dist/StatusSummary.d.ts.map +1 -1
- package/dist/StatusSummary.js +6 -5
- package/dist/StatusSummary.js.map +1 -1
- package/dist/ToolStatus.d.ts +32 -29
- package/dist/ToolStatus.d.ts.map +1 -1
- package/dist/ToolStatus.js +42 -27
- package/dist/ToolStatus.js.map +1 -1
- package/dist/internal/live.js +3 -1
- package/dist/internal/live.js.map +1 -1
- package/dist/internal/loop.d.ts.map +1 -1
- package/dist/internal/loop.js +5 -26
- package/dist/internal/loop.js.map +1 -1
- package/dist/internal/summary.js +24 -5
- package/dist/internal/summary.js.map +1 -1
- package/package.json +1 -1
- package/src/StatusSummary.ts +8 -6
- package/src/ToolStatus.ts +66 -41
- package/src/internal/live.ts +4 -1
- package/src/internal/loop.ts +14 -53
- package/src/internal/summary.ts +27 -7
package/dist/StatusSummary.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ declare namespace StatusSummary_d_exports {
|
|
|
13
13
|
* @category constants
|
|
14
14
|
* @since 0.13.0
|
|
15
15
|
*/
|
|
16
|
-
export declare const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read
|
|
16
|
+
export declare const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
17
17
|
/**
|
|
18
18
|
* Represents the options accepted by {@link layer}.
|
|
19
19
|
*
|
|
@@ -32,8 +32,9 @@ declare const StatusSummary_base: Context.ServiceClass<StatusSummary, "effect-as
|
|
|
32
32
|
readonly after: Duration.Duration;
|
|
33
33
|
readonly staleAfter: Duration.Duration;
|
|
34
34
|
/**
|
|
35
|
-
* Returns the status for a turn, from its transcript:
|
|
36
|
-
*
|
|
35
|
+
* Returns the status for a turn, from its transcript: this turn after a few earlier messages,
|
|
36
|
+
* what still runs, and the lines shown so far. `None` when the model has nothing new, wrote
|
|
37
|
+
* nothing usable, or its call failed.
|
|
37
38
|
*/
|
|
38
39
|
readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>;
|
|
39
40
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StatusSummary.d.ts","names":[],"sources":["../src/StatusSummary.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"StatusSummary.d.ts","names":[],"sources":["../src/StatusSummary.ts"],"mappings":";;;;;;;;;;;;;;;qBA6Ba;;;;;;;iBASI;;WAEN,QAAQ,SAAS;;WAEjB,aAAa,SAAS;;WAEtB;;;WAYS,OAAA,SAAS;WACJ,YAAA,SAAS;;;;;;WAMV,YAAC,uBAAuB,OAAO,OAAO,OAAO;;;;;;;;qBAVxD,sBAAsB;;;;;;;;;;;;;;;;;;;;;qBA6CtB,QAAK,UAAc,YAAU,MAAM,MAAM,sBAAsB,cAAc"}
|
package/dist/StatusSummary.js
CHANGED
|
@@ -16,8 +16,9 @@ import * as LanguageModel from "effect/ai/LanguageModel";
|
|
|
16
16
|
* Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary
|
|
17
17
|
* is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.
|
|
18
18
|
* It never replaces the status of a described call that still runs, never shows while the model
|
|
19
|
-
* writes to the user, and never after the turn's message unless the model starts new work.
|
|
20
|
-
*
|
|
19
|
+
* writes to the user, and never after the turn's message unless the model starts new work. The
|
|
20
|
+
* model reads this turn, after a few earlier messages as context, and the lines the turn showed so
|
|
21
|
+
* far, and answers `-` when nothing new happened since: then the line stays as it is.
|
|
21
22
|
*
|
|
22
23
|
* @since 0.13.0
|
|
23
24
|
*/
|
|
@@ -32,7 +33,7 @@ var StatusSummary_exports = /* @__PURE__ */ __exportAll({
|
|
|
32
33
|
* @category constants
|
|
33
34
|
* @since 0.13.0
|
|
34
35
|
*/
|
|
35
|
-
const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read
|
|
36
|
+
const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
36
37
|
/**
|
|
37
38
|
* Service that writes a turn's status from what the turn did so far.
|
|
38
39
|
*
|
|
@@ -43,7 +44,7 @@ var StatusSummary = class extends Context.Service()("effect-assistant/StatusSumm
|
|
|
43
44
|
const MAX_LENGTH = 60;
|
|
44
45
|
const clean = (text) => {
|
|
45
46
|
const line = (text.trim().split("\n")[0] ?? "").replace(/^["'“”‘’]+|["'“”‘’]+$/g, "").replace(/[.…]+$/, "").trim();
|
|
46
|
-
return line === "" || line.length > MAX_LENGTH ? Option.none() : Option.some(line);
|
|
47
|
+
return line === "" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line);
|
|
47
48
|
};
|
|
48
49
|
/**
|
|
49
50
|
* Creates the summary on the model it is given: provide a small, fast one.
|
|
@@ -67,7 +68,7 @@ const clean = (text) => {
|
|
|
67
68
|
*/
|
|
68
69
|
const layer = (options) => Layer.effect(StatusSummary, Effect.gen(function* () {
|
|
69
70
|
const model = yield* LanguageModel.LanguageModel;
|
|
70
|
-
const instructions = options?.instructions ?? "You write the status line a chat shows while an assistant works for its user. Read
|
|
71
|
+
const instructions = options?.instructions ?? "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
71
72
|
const summarize = Effect.fn("StatusSummary.summarize")(function* (transcript) {
|
|
72
73
|
const response = yield* model.generateText({ prompt: Prompt.fromMessages([Prompt.systemMessage({ content: instructions }), Prompt.userMessage({ content: [Prompt.textPart({ text: transcript })] })]) });
|
|
73
74
|
return clean(response.text);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StatusSummary.js","names":[],"sources":["../src/StatusSummary.ts"],"sourcesContent":["/**\n * This module provides the status a turn shows when nothing else says what it is doing: when the\n * line stays empty for a while, or a call's status still shows after that call ended, a model reads\n * the turn so far and writes a few words about it, like \"Comparing last year's budgets\".\n *\n * Provide {@link layer} next to the assistant's `toLayer`, with a small, fast model of its own.\n * Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary\n * is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.\n * It never replaces the status of a described call that still runs, never shows while the model\n * writes to the user, and never after the turn's message unless the model starts new work.
|
|
1
|
+
{"version":3,"file":"StatusSummary.js","names":[],"sources":["../src/StatusSummary.ts"],"sourcesContent":["/**\n * This module provides the status a turn shows when nothing else says what it is doing: when the\n * line stays empty for a while, or a call's status still shows after that call ended, a model reads\n * the turn so far and writes a few words about it, like \"Comparing last year's budgets\".\n *\n * Provide {@link layer} next to the assistant's `toLayer`, with a small, fast model of its own.\n * Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary\n * is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.\n * It never replaces the status of a described call that still runs, never shows while the model\n * writes to the user, and never after the turn's message unless the model starts new work. The\n * model reads this turn, after a few earlier messages as context, and the lines the turn showed so\n * far, and answers `-` when nothing new happened since: then the line stays as it is.\n *\n * @since 0.13.0\n */\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport * as Context from \"effect/Context\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\n\n/**\n * The instructions the summary's model gets by default.\n *\n * @category constants\n * @since 0.13.0\n */\nexport const defaultInstructions =\n 'You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year\\'s budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.'\n\n/**\n * Represents the options accepted by {@link layer}.\n *\n * @category models\n * @since 0.13.0\n */\nexport interface Options {\n /** How long the line may stay empty before a summary fills it. Defaults to 2 seconds. */\n readonly after?: Duration.Input | undefined\n /** How long a line may stay unchanged before a summary refreshes it. Defaults to 5 seconds. */\n readonly staleAfter?: Duration.Input | undefined\n /** The summary model's instructions. Defaults to {@link defaultInstructions}. */\n readonly instructions?: string | undefined\n}\n\n/**\n * Service that writes a turn's status from what the turn did so far.\n *\n * @category services\n * @since 0.13.0\n */\nexport class StatusSummary extends Context.Service<\n StatusSummary,\n {\n readonly after: Duration.Duration\n readonly staleAfter: Duration.Duration\n /**\n * Returns the status for a turn, from its transcript: this turn after a few earlier messages,\n * what still runs, and the lines shown so far. `None` when the model has nothing new, wrote\n * nothing usable, or its call failed.\n */\n readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>\n }\n>()(\"effect-assistant/StatusSummary\") {}\n\nconst MAX_LENGTH = 60\n\nconst clean = (text: string): Option.Option<string> => {\n const line = (text.trim().split(\"\\n\")[0] ?? \"\")\n .replace(/^[\"'“”‘’]+|[\"'“”‘’]+$/g, \"\")\n .replace(/[.…]+$/, \"\")\n .trim()\n\n return line === \"\" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line)\n}\n\n/**\n * Creates the summary on the model it is given: provide a small, fast one.\n *\n * **Example** (A cheap model for statuses)\n *\n * ```ts\n * import { StatusSummary } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const MyAssistantLive: Layer.Layer<never>\n * declare const FastModel: Layer.Layer<import(\"effect/ai/LanguageModel\").LanguageModel>\n *\n * export const Live = MyAssistantLive.pipe(\n * Layer.provide(StatusSummary.layer({ after: \"2 seconds\" }).pipe(Layer.provide(FastModel)))\n * )\n * ```\n *\n * @category layers\n * @since 0.13.0\n */\nexport const layer = (options?: Options): Layer.Layer<StatusSummary, never, LanguageModel.LanguageModel> =>\n Layer.effect(\n StatusSummary,\n Effect.gen(function* () {\n const model = yield* LanguageModel.LanguageModel\n const instructions = options?.instructions ?? defaultInstructions\n\n const summarize = Effect.fn(\"StatusSummary.summarize\")(function* (transcript: string) {\n const response = yield* model.generateText({\n prompt: Prompt.fromMessages([\n Prompt.systemMessage({ content: instructions }),\n Prompt.userMessage({ content: [Prompt.textPart({ text: transcript })] })\n ])\n })\n\n return clean(response.text)\n })\n\n return StatusSummary.of({\n after: Duration.fromInputUnsafe(options?.after ?? \"2 seconds\"),\n staleAfter: Duration.fromInputUnsafe(options?.staleAfter ?? \"5 seconds\"),\n summarize: (transcript) =>\n summarize(transcript).pipe(\n Effect.catchCause((cause) =>\n Effect.as(Effect.logDebug(\"assistant status summary failed\", cause), Option.none())\n )\n )\n })\n })\n )\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,sBACX;;;;;;;AAuBF,IAAa,gBAAb,cAAmC,QAAQ,QAYzC,CAAC,CAAC,gCAAgC,CAAC,CAAC,CAAC;AAEvC,MAAM,aAAa;AAEnB,MAAM,SAAS,SAAwC;CACrD,MAAM,QAAQ,KAAK,KAAK,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,GAAA,CACzC,QAAQ,0BAA0B,EAAE,CAAC,CACrC,QAAQ,UAAU,EAAE,CAAC,CACrB,KAAK;CAER,OAAO,SAAS,MAAM,WAAW,KAAK,IAAI,KAAK,KAAK,SAAS,aAAa,OAAO,KAAK,IAAI,OAAO,KAAK,IAAI;AAC5G;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,SAAS,YACpB,MAAM,OACJ,eACA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,OAAO,cAAc;CACnC,MAAM,eAAe,SAAS,gBAAA;CAE9B,MAAM,YAAY,OAAO,GAAG,yBAAyB,CAAC,CAAC,WAAW,YAAoB;EACpF,MAAM,WAAW,OAAO,MAAM,aAAa,EACzC,QAAQ,OAAO,aAAa,CAC1B,OAAO,cAAc,EAAE,SAAS,aAAa,CAAC,GAC9C,OAAO,YAAY,EAAE,SAAS,CAAC,OAAO,SAAS,EAAE,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,CACzE,CAAC,EACH,CAAC;EAED,OAAO,MAAM,SAAS,IAAI;CAC5B,CAAC;CAED,OAAO,cAAc,GAAG;EACtB,OAAO,SAAS,gBAAgB,SAAS,SAAS,WAAW;EAC7D,YAAY,SAAS,gBAAgB,SAAS,cAAc,WAAW;EACvE,YAAY,eACV,UAAU,UAAU,CAAC,CAAC,KACpB,OAAO,YAAY,UACjB,OAAO,GAAG,OAAO,SAAS,mCAAmC,KAAK,GAAG,OAAO,KAAK,CAAC,CACpF,CACF;CACJ,CAAC;AACH,CAAC,CACH"}
|
package/dist/ToolStatus.d.ts
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import * as Context from "effect/Context";
|
|
2
1
|
import * as Schema from "effect/Schema";
|
|
3
2
|
import * as Tool from "effect/ai/Tool";
|
|
4
3
|
declare namespace ToolStatus_d_exports {
|
|
5
|
-
export {
|
|
4
|
+
export { StatusField, WithoutStatus, isDescribed, parameterDescription, parameterName, withStatus };
|
|
6
5
|
}
|
|
7
|
-
type Schemas = {
|
|
8
|
-
readonly parameters: Schema.Constraint;
|
|
9
|
-
readonly success: Schema.Constraint;
|
|
10
|
-
readonly failure: Schema.Constraint;
|
|
11
|
-
readonly failureMode: Tool.FailureMode;
|
|
12
|
-
};
|
|
13
6
|
/**
|
|
14
7
|
* The name of the parameter the model fills with a call's status.
|
|
15
8
|
*
|
|
@@ -25,44 +18,54 @@ export declare const parameterName = "description";
|
|
|
25
18
|
*/
|
|
26
19
|
export declare const parameterDescription = "Shown to the user while this runs: 3 to 8 plain words in the user's language about what you're doing, like \"Checking last summer's campaign budget\". No tool names, ids or code.";
|
|
27
20
|
/**
|
|
28
|
-
*
|
|
21
|
+
* The schema of the status field {@link withStatus} adds.
|
|
29
22
|
*
|
|
30
|
-
* @category
|
|
31
|
-
* @since 0.
|
|
23
|
+
* @category schemas
|
|
24
|
+
* @since 0.16.0
|
|
32
25
|
*/
|
|
33
|
-
export declare const
|
|
26
|
+
export declare const StatusField: Schema.String;
|
|
34
27
|
/**
|
|
35
|
-
*
|
|
28
|
+
* The parameter fields a struct can't have to take a status field: a `description` field of its own.
|
|
29
|
+
*
|
|
30
|
+
* @category models
|
|
31
|
+
* @since 0.16.0
|
|
32
|
+
*/
|
|
33
|
+
export type WithoutStatus<Fields extends Schema.Struct.Fields> = typeof parameterName extends keyof Fields ? "Error: these parameters have a description field already, so they can't take a status: rename it" : unknown;
|
|
34
|
+
/**
|
|
35
|
+
* Adds the status field to a tool's parameters, so its calls show a status while they run.
|
|
36
36
|
*
|
|
37
37
|
* **Details**
|
|
38
38
|
*
|
|
39
|
-
* The
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
39
|
+
* The struct gets a required `description` field ({@link StatusField}), and keeps its own
|
|
40
|
+
* annotations and checks. Required, because models leave optional parameters out. The chat shows
|
|
41
|
+
* what the model writes there while the call runs; a call with a blank one runs with no status,
|
|
42
|
+
* and a call without it fails validation like a call without any other required parameter. The
|
|
43
|
+
* handler gets `description` like any other parameter. A struct that has a `description` field
|
|
44
|
+
* already is a type error. A tool without parameters takes `Schema.Struct({})`.
|
|
43
45
|
*
|
|
44
|
-
* **Example** (A
|
|
46
|
+
* **Example** (A tool whose calls show a status)
|
|
45
47
|
*
|
|
46
48
|
* ```ts
|
|
47
49
|
* import { ToolStatus } from "effect-assistant"
|
|
48
50
|
* import * as Tool from "effect/ai/Tool"
|
|
49
51
|
* import * as Schema from "effect/Schema"
|
|
50
52
|
*
|
|
51
|
-
* export const
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* })
|
|
57
|
-
* )
|
|
53
|
+
* export const ArchiveNote = Tool.make("archive_note", {
|
|
54
|
+
* description: "Archives a note and the files attached to it.",
|
|
55
|
+
* parameters: Schema.Struct({ noteId: Schema.String }).pipe(ToolStatus.withStatus),
|
|
56
|
+
* success: Schema.Struct({ archived: Schema.Boolean })
|
|
57
|
+
* })
|
|
58
58
|
* ```
|
|
59
59
|
*
|
|
60
|
-
* @category
|
|
61
|
-
* @since 0.
|
|
60
|
+
* @category combinators
|
|
61
|
+
* @since 0.16.0
|
|
62
62
|
*/
|
|
63
|
-
export declare const
|
|
63
|
+
export declare const withStatus: <Fields extends Schema.Struct.Fields>(self: Schema.Struct<Fields> & WithoutStatus<Fields>) => Schema.Struct<Readonly<Fields & {
|
|
64
|
+
description: Schema.String;
|
|
65
|
+
}> extends (infer T) ? { [K in keyof T]: T[K]; } : never>;
|
|
64
66
|
/**
|
|
65
|
-
* Returns whether a tool's calls show a status
|
|
67
|
+
* Returns whether a tool's calls show a status: its parameters have the field {@link withStatus}
|
|
68
|
+
* adds. A `description` parameter of its own doesn't count.
|
|
66
69
|
*
|
|
67
70
|
* @category guards
|
|
68
71
|
* @since 0.13.0
|
package/dist/ToolStatus.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolStatus.d.ts","names":[],"sources":["../src/ToolStatus.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"ToolStatus.d.ts","names":[],"sources":["../src/ToolStatus.ts"],"mappings":";;;;;;;;;;;qBAuBa;;;;;;;qBAQA;;;;;;;qBAWA,aAAW,OAAA;;;;;;;YAQZ,cAAc,eAAe,OAAO,OAAO,iBAAiB,4BAA4B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAiCvF,aAAc,eAAe,OAAO,OAAO,QAAM,MACtD,OAAO,OAAO,UAAU,cAAc,YAAO,OAAA,OAAA,SAAA;;kBAYpD,QAAA,WAAA,IAAA,EAAA;;;;;;;;qBASY,cAAW,MAAU,KAAK"}
|
package/dist/ToolStatus.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import * as SchemaAST from "effect/SchemaAST";
|
|
3
4
|
//#region src/ToolStatus.ts
|
|
4
5
|
var ToolStatus_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
-
|
|
6
|
-
described: () => described,
|
|
6
|
+
StatusField: () => StatusField,
|
|
7
7
|
isDescribed: () => isDescribed,
|
|
8
8
|
parameterDescription: () => parameterDescription,
|
|
9
|
-
parameterName: () => parameterName
|
|
9
|
+
parameterName: () => parameterName,
|
|
10
|
+
withStatus: () => withStatus
|
|
10
11
|
});
|
|
11
12
|
/**
|
|
12
13
|
* The name of the parameter the model fills with a call's status.
|
|
@@ -22,51 +23,65 @@ const parameterName = "description";
|
|
|
22
23
|
* @since 0.13.0
|
|
23
24
|
*/
|
|
24
25
|
const parameterDescription = "Shown to the user while this runs: 3 to 8 plain words in the user's language about what you're doing, like \"Checking last summer's campaign budget\". No tool names, ids or code.";
|
|
26
|
+
const statusFieldKey = "effect-assistant/ToolStatus/statusField";
|
|
25
27
|
/**
|
|
26
|
-
*
|
|
28
|
+
* The schema of the status field {@link withStatus} adds.
|
|
27
29
|
*
|
|
28
|
-
* @category
|
|
29
|
-
* @since 0.
|
|
30
|
+
* @category schemas
|
|
31
|
+
* @since 0.16.0
|
|
30
32
|
*/
|
|
31
|
-
const
|
|
33
|
+
const StatusField = Schema.String.annotate({
|
|
34
|
+
description: parameterDescription,
|
|
35
|
+
[statusFieldKey]: true
|
|
36
|
+
});
|
|
32
37
|
/**
|
|
33
|
-
*
|
|
38
|
+
* Adds the status field to a tool's parameters, so its calls show a status while they run.
|
|
34
39
|
*
|
|
35
40
|
* **Details**
|
|
36
41
|
*
|
|
37
|
-
* The
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
42
|
+
* The struct gets a required `description` field ({@link StatusField}), and keeps its own
|
|
43
|
+
* annotations and checks. Required, because models leave optional parameters out. The chat shows
|
|
44
|
+
* what the model writes there while the call runs; a call with a blank one runs with no status,
|
|
45
|
+
* and a call without it fails validation like a call without any other required parameter. The
|
|
46
|
+
* handler gets `description` like any other parameter. A struct that has a `description` field
|
|
47
|
+
* already is a type error. A tool without parameters takes `Schema.Struct({})`.
|
|
41
48
|
*
|
|
42
|
-
* **Example** (A
|
|
49
|
+
* **Example** (A tool whose calls show a status)
|
|
43
50
|
*
|
|
44
51
|
* ```ts
|
|
45
52
|
* import { ToolStatus } from "effect-assistant"
|
|
46
53
|
* import * as Tool from "effect/ai/Tool"
|
|
47
54
|
* import * as Schema from "effect/Schema"
|
|
48
55
|
*
|
|
49
|
-
* export const
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* })
|
|
55
|
-
* )
|
|
56
|
+
* export const ArchiveNote = Tool.make("archive_note", {
|
|
57
|
+
* description: "Archives a note and the files attached to it.",
|
|
58
|
+
* parameters: Schema.Struct({ noteId: Schema.String }).pipe(ToolStatus.withStatus),
|
|
59
|
+
* success: Schema.Struct({ archived: Schema.Boolean })
|
|
60
|
+
* })
|
|
56
61
|
* ```
|
|
57
62
|
*
|
|
58
|
-
* @category
|
|
59
|
-
* @since 0.
|
|
63
|
+
* @category combinators
|
|
64
|
+
* @since 0.16.0
|
|
60
65
|
*/
|
|
61
|
-
const
|
|
66
|
+
const withStatus = (self) => {
|
|
67
|
+
const struct = self.mapFields((fields) => ({
|
|
68
|
+
...fields,
|
|
69
|
+
[parameterName]: StatusField
|
|
70
|
+
}));
|
|
71
|
+
return struct.rebuild(new SchemaAST.Objects(struct.ast.propertySignatures, struct.ast.indexSignatures, self.ast.annotations, self.ast.checks));
|
|
72
|
+
};
|
|
62
73
|
/**
|
|
63
|
-
* Returns whether a tool's calls show a status
|
|
74
|
+
* Returns whether a tool's calls show a status: its parameters have the field {@link withStatus}
|
|
75
|
+
* adds. A `description` parameter of its own doesn't count.
|
|
64
76
|
*
|
|
65
77
|
* @category guards
|
|
66
78
|
* @since 0.13.0
|
|
67
79
|
*/
|
|
68
|
-
const isDescribed = (tool) =>
|
|
80
|
+
const isDescribed = (tool) => {
|
|
81
|
+
const ast = tool.parametersSchema.ast;
|
|
82
|
+
return SchemaAST.isObjects(ast) && ast.propertySignatures.some((property) => property.name === "description" && SchemaAST.resolveAt(statusFieldKey)(property.type) === true);
|
|
83
|
+
};
|
|
69
84
|
//#endregion
|
|
70
|
-
export {
|
|
85
|
+
export { StatusField, ToolStatus_exports, isDescribed, parameterDescription, parameterName, withStatus };
|
|
71
86
|
|
|
72
87
|
//# sourceMappingURL=ToolStatus.js.map
|
package/dist/ToolStatus.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolStatus.js","names":[],"sources":["../src/ToolStatus.ts"],"sourcesContent":["/**\n * This module provides status labels for tool calls: while a call runs, the chat shows the user a\n * short line about what the assistant is doing, like \"Checking last summer's campaign budget\".\n *\n *
|
|
1
|
+
{"version":3,"file":"ToolStatus.js","names":[],"sources":["../src/ToolStatus.ts"],"sourcesContent":["/**\n * This module provides status labels for tool calls: while a call runs, the chat shows the user a\n * short line about what the assistant is doing, like \"Checking last summer's campaign budget\".\n *\n * Give the tools that take a while or act on something a status field with {@link withStatus}:\n * workflows, reports from other systems, orders, specialists. Their parameters get a required\n * `description` field that the model fills in the chat's language. When the call starts, the chat\n * sends the description as a `ChatSignal.Status`; it shows until the next status, so it stays\n * after the call ends. The call runs unchanged: the handler gets `description` with its other\n * parameters. Quick reads need no label: the chat shows its generic working state for them.\n *\n * @since 0.13.0\n */\nimport type * as Tool from \"effect/ai/Tool\"\nimport * as Schema from \"effect/Schema\"\nimport * as SchemaAST from \"effect/SchemaAST\"\n\n/**\n * The name of the parameter the model fills with a call's status.\n *\n * @category constants\n * @since 0.13.0\n */\nexport const parameterName = \"description\"\n\n/**\n * What the model reads about the parameter.\n *\n * @category constants\n * @since 0.13.0\n */\nexport const parameterDescription =\n \"Shown to the user while this runs: 3 to 8 plain words in the user's language about what you're doing, like \\\"Checking last summer's campaign budget\\\". No tool names, ids or code.\"\n\nconst statusFieldKey = \"effect-assistant/ToolStatus/statusField\"\n\n/**\n * The schema of the status field {@link withStatus} adds.\n *\n * @category schemas\n * @since 0.16.0\n */\nexport const StatusField = Schema.String.annotate({ description: parameterDescription, [statusFieldKey]: true })\n\n/**\n * The parameter fields a struct can't have to take a status field: a `description` field of its own.\n *\n * @category models\n * @since 0.16.0\n */\nexport type WithoutStatus<Fields extends Schema.Struct.Fields> = typeof parameterName extends keyof Fields\n ? \"Error: these parameters have a description field already, so they can't take a status: rename it\"\n : unknown\n\n/**\n * Adds the status field to a tool's parameters, so its calls show a status while they run.\n *\n * **Details**\n *\n * The struct gets a required `description` field ({@link StatusField}), and keeps its own\n * annotations and checks. Required, because models leave optional parameters out. The chat shows\n * what the model writes there while the call runs; a call with a blank one runs with no status,\n * and a call without it fails validation like a call without any other required parameter. The\n * handler gets `description` like any other parameter. A struct that has a `description` field\n * already is a type error. A tool without parameters takes `Schema.Struct({})`.\n *\n * **Example** (A tool whose calls show a status)\n *\n * ```ts\n * import { ToolStatus } from \"effect-assistant\"\n * import * as Tool from \"effect/ai/Tool\"\n * import * as Schema from \"effect/Schema\"\n *\n * export const ArchiveNote = Tool.make(\"archive_note\", {\n * description: \"Archives a note and the files attached to it.\",\n * parameters: Schema.Struct({ noteId: Schema.String }).pipe(ToolStatus.withStatus),\n * success: Schema.Struct({ archived: Schema.Boolean })\n * })\n * ```\n *\n * @category combinators\n * @since 0.16.0\n */\nexport const withStatus = <Fields extends Schema.Struct.Fields>(\n self: Schema.Struct<Fields> & WithoutStatus<Fields>\n) => {\n const struct = self.mapFields((fields) => ({ ...fields, [parameterName]: StatusField }))\n\n return struct.rebuild(\n new SchemaAST.Objects(\n struct.ast.propertySignatures,\n struct.ast.indexSignatures,\n self.ast.annotations,\n self.ast.checks\n )\n )\n}\n\n/**\n * Returns whether a tool's calls show a status: its parameters have the field {@link withStatus}\n * adds. A `description` parameter of its own doesn't count.\n *\n * @category guards\n * @since 0.13.0\n */\nexport const isDescribed = (tool: Tool.Any): boolean => {\n const ast = tool.parametersSchema.ast\n\n return (\n SchemaAST.isObjects(ast) &&\n ast.propertySignatures.some(\n (property) =>\n property.name === parameterName && SchemaAST.resolveAt<boolean>(statusFieldKey)(property.type) === true\n )\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAuBA,MAAa,gBAAgB;;;;;;;AAQ7B,MAAa,uBACX;AAEF,MAAM,iBAAiB;;;;;;;AAQvB,MAAa,cAAc,OAAO,OAAO,SAAS;CAAE,aAAa;EAAuB,iBAAiB;AAAK,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyC/G,MAAa,cACX,SACG;CACH,MAAM,SAAS,KAAK,WAAW,YAAY;EAAE,GAAG;GAAS,gBAAgB;CAAY,EAAE;CAEvF,OAAO,OAAO,QACZ,IAAI,UAAU,QACZ,OAAO,IAAI,oBACX,OAAO,IAAI,iBACX,KAAK,IAAI,aACT,KAAK,IAAI,MACX,CACF;AACF;;;;;;;;AASA,MAAa,eAAe,SAA4B;CACtD,MAAM,MAAM,KAAK,iBAAiB;CAElC,OACE,UAAU,UAAU,GAAG,KACvB,IAAI,mBAAmB,MACpB,aACC,SAAS,SAAA,iBAA0B,UAAU,UAAmB,cAAc,CAAC,CAAC,SAAS,IAAI,MAAM,IACvG;AAEJ"}
|
package/dist/internal/live.js
CHANGED
|
@@ -18,6 +18,7 @@ const make = Effect.fnUntraced(function* (send) {
|
|
|
18
18
|
running: [],
|
|
19
19
|
shown: void 0,
|
|
20
20
|
shownAt: yield* Clock.currentTimeMillis,
|
|
21
|
+
lines: [],
|
|
21
22
|
typing: false
|
|
22
23
|
});
|
|
23
24
|
const change = (f) => SynchronizedRef.updateEffect(state, Effect.fnUntraced(function* (current) {
|
|
@@ -30,7 +31,8 @@ const make = Effect.fnUntraced(function* (send) {
|
|
|
30
31
|
const show = (current, text, now) => current.shown === text ? [[], current] : [[Status.make({ text })], {
|
|
31
32
|
...current,
|
|
32
33
|
shown: text,
|
|
33
|
-
shownAt: now
|
|
34
|
+
shownAt: now,
|
|
35
|
+
lines: [...current.lines, text]
|
|
34
36
|
}];
|
|
35
37
|
return {
|
|
36
38
|
callStarted: (callId, text) => change((current, now) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"live.js","names":[],"sources":["../../src/internal/live.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\n\ninterface Labelled {\n readonly callId: string\n readonly text: string\n}\n\n/** @internal */\nexport interface State {\n /** The described calls still running, oldest first. */\n readonly running: ReadonlyArray<Labelled>\n /** The line the user sees; none before the first status and after typing started. */\n readonly shown: string | undefined\n /** When the line last changed, or the turn started, in epoch milliseconds. */\n readonly shownAt: number\n readonly typing: boolean\n}\n\n/** @internal */\nexport interface Live {\n readonly callStarted: (callId: string, text: string) => Effect.Effect<void>\n readonly callEnded: (callId: string) => Effect.Effect<void>\n // A summary's line: shown unless the model started a message, or the line says it already.\n readonly summarized: (text: string) => Effect.Effect<void>\n readonly typing: (typing: boolean) => Effect.Effect<void>\n readonly state: Effect.Effect<State>\n // The turn ends: typing stops. Its end, in the log, clears the line.\n readonly reset: Effect.Effect<void>\n}\n\n// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.\n/** @internal */\nexport const TurnLive = Context.Reference<Option.Option<Live>>(\"effect-assistant/TurnLive\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {\n const live = yield* TurnLive\n\n if (Option.isSome(live)) yield* f(live.value)\n})\n\ntype Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {\n const state = yield* SynchronizedRef.make<State>({\n running: [],\n shown: undefined,\n shownAt: yield* Clock.currentTimeMillis,\n typing: false\n })\n\n // Under the ref's lock, so signals go out in the order the state changed.\n const change = (f: (current: State, now: number) => Change) =>\n SynchronizedRef.updateEffect(\n state,\n Effect.fnUntraced(function* (current) {\n const changed = f(current, yield* Clock.currentTimeMillis)\n\n if (changed === undefined) return current\n\n const [signals, next] = changed\n\n yield* Effect.forEach(signals, send, { discard: true })\n\n return next\n })\n )\n\n const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>\n current.shown === text\n ? [[], current]\n : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]\n\n const live: Live = {\n callStarted: (callId, text) =>\n change((current, now) => {\n const running = { ...current, running: [...current.running, { callId, text }] }\n\n return current.typing ? [[], running] : show(running, text, now)\n }),\n // The line stays when the last labelled call ends; while another runs, the line says that one.\n callEnded: (callId) =>\n change((current, now) => {\n const ended = current.running.find((call) => call.callId === callId)\n\n if (ended === undefined) return undefined\n\n const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }\n const still = next.running.at(-1)\n\n return still === undefined || current.typing || current.shown !== ended.text\n ? [[], next]\n : show(next, still.text, now)\n }),\n summarized: (text) =>\n change((current, now) =>\n current.typing\n ? undefined\n : current.shown === text\n ? [[], { ...current, shownAt: now }]\n : show(current, text, now)\n ),\n typing: (typing) =>\n change((current, now) => {\n if (current.typing === typing) return undefined\n\n if (typing) {\n return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]\n }\n\n // A call that runs on after the message still says what it does.\n const stopped = { ...current, typing, shownAt: now }\n const still = current.running.at(-1)\n const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)\n\n return [[ChatSignal.TypingStopped.make({}), ...signals], next]\n }),\n state: SynchronizedRef.get(state),\n reset: change((current) =>\n current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined\n )\n }\n\n return live\n})\n"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"live.js","names":[],"sources":["../../src/internal/live.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\n\ninterface Labelled {\n readonly callId: string\n readonly text: string\n}\n\n/** @internal */\nexport interface State {\n /** The described calls still running, oldest first. */\n readonly running: ReadonlyArray<Labelled>\n /** The line the user sees; none before the first status and after typing started. */\n readonly shown: string | undefined\n /** When the line last changed, or the turn started, in epoch milliseconds. */\n readonly shownAt: number\n /** Every line the turn showed, oldest first. */\n readonly lines: ReadonlyArray<string>\n readonly typing: boolean\n}\n\n/** @internal */\nexport interface Live {\n readonly callStarted: (callId: string, text: string) => Effect.Effect<void>\n readonly callEnded: (callId: string) => Effect.Effect<void>\n // A summary's line: shown unless the model started a message, or the line says it already.\n readonly summarized: (text: string) => Effect.Effect<void>\n readonly typing: (typing: boolean) => Effect.Effect<void>\n readonly state: Effect.Effect<State>\n // The turn ends: typing stops. Its end, in the log, clears the line.\n readonly reset: Effect.Effect<void>\n}\n\n// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.\n/** @internal */\nexport const TurnLive = Context.Reference<Option.Option<Live>>(\"effect-assistant/TurnLive\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {\n const live = yield* TurnLive\n\n if (Option.isSome(live)) yield* f(live.value)\n})\n\ntype Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {\n const state = yield* SynchronizedRef.make<State>({\n running: [],\n shown: undefined,\n shownAt: yield* Clock.currentTimeMillis,\n lines: [],\n typing: false\n })\n\n // Under the ref's lock, so signals go out in the order the state changed.\n const change = (f: (current: State, now: number) => Change) =>\n SynchronizedRef.updateEffect(\n state,\n Effect.fnUntraced(function* (current) {\n const changed = f(current, yield* Clock.currentTimeMillis)\n\n if (changed === undefined) return current\n\n const [signals, next] = changed\n\n yield* Effect.forEach(signals, send, { discard: true })\n\n return next\n })\n )\n\n const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>\n current.shown === text\n ? [[], current]\n : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now, lines: [...current.lines, text] }]\n\n const live: Live = {\n callStarted: (callId, text) =>\n change((current, now) => {\n const running = { ...current, running: [...current.running, { callId, text }] }\n\n return current.typing ? [[], running] : show(running, text, now)\n }),\n // The line stays when the last labelled call ends; while another runs, the line says that one.\n callEnded: (callId) =>\n change((current, now) => {\n const ended = current.running.find((call) => call.callId === callId)\n\n if (ended === undefined) return undefined\n\n const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }\n const still = next.running.at(-1)\n\n return still === undefined || current.typing || current.shown !== ended.text\n ? [[], next]\n : show(next, still.text, now)\n }),\n summarized: (text) =>\n change((current, now) =>\n current.typing\n ? undefined\n : current.shown === text\n ? [[], { ...current, shownAt: now }]\n : show(current, text, now)\n ),\n typing: (typing) =>\n change((current, now) => {\n if (current.typing === typing) return undefined\n\n if (typing) {\n return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]\n }\n\n // A call that runs on after the message still says what it does.\n const stopped = { ...current, typing, shownAt: now }\n const still = current.running.at(-1)\n const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)\n\n return [[ChatSignal.TypingStopped.make({}), ...signals], next]\n }),\n state: SynchronizedRef.get(state),\n reset: change((current) =>\n current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined\n )\n }\n\n return live\n})\n"],"mappings":";;;;;;;;AAuCA,MAAa,WAAW,QAAQ,UAA+B,6BAA6B,EAC1F,cAAc,OAAO,KACvB,CAAC;;AAGD,MAAa,MAAM,OAAO,WAAW,WAAW,GAAwC;CACtF,MAAM,OAAO,OAAO;CAEpB,IAAI,OAAO,OAAO,IAAI,GAAG,OAAO,EAAE,KAAK,KAAK;AAC9C,CAAC;;AAKD,MAAa,OAAO,OAAO,WAAW,WAAW,MAA8D;CAC7G,MAAM,QAAQ,OAAO,gBAAgB,KAAY;EAC/C,SAAS,CAAC;EACV,OAAO,KAAA;EACP,SAAS,OAAO,MAAM;EACtB,OAAO,CAAC;EACR,QAAQ;CACV,CAAC;CAGD,MAAM,UAAU,MACd,gBAAgB,aACd,OACA,OAAO,WAAW,WAAW,SAAS;EACpC,MAAM,UAAU,EAAE,SAAS,OAAO,MAAM,iBAAiB;EAEzD,IAAI,YAAY,KAAA,GAAW,OAAO;EAElC,MAAM,CAAC,SAAS,QAAQ;EAExB,OAAO,OAAO,QAAQ,SAAS,MAAM,EAAE,SAAS,KAAK,CAAC;EAEtD,OAAO;CACT,CAAC,CACH;CAEF,MAAM,QAAQ,SAAgB,MAAc,QAC1C,QAAQ,UAAU,OACd,CAAC,CAAC,GAAG,OAAO,IACZ,CAAC,CAAA,OAAmB,KAAK,EAAE,KAAK,CAAC,CAAC,GAAG;EAAE,GAAG;EAAS,OAAO;EAAM,SAAS;EAAK,OAAO,CAAC,GAAG,QAAQ,OAAO,IAAI;CAAE,CAAC;CAoDrH,OAAO;EAjDL,cAAc,QAAQ,SACpB,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;GAE3F,OAAO,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,GAAG,GAAG,OAAO,GAAG,IAAI;EAC/D,CAAC;EACH,OAAO,gBAAgB,IAAI,KAAK;EAChC,OAAO,QAAQ,YACb,QAAQ,SAAS,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,CAAC,GAAG;GAAE,GAAG;GAAS,QAAQ;EAAM,CAAC,IAAI,KAAA,CAC1F;CAGQ;AACZ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","names":[],"sources":["../../src/internal/loop.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"loop.d.ts","names":[],"sources":["../../src/internal/loop.ts"],"mappings":";;;;;;;;;;;;;;;YA0BY,QACR,cACA,cACA,aACA;;qBAKS,eAAa,QAAA,UAAA,OAAA;WACW,UAAA,OAAO,OAAO,cAAc;;;qBAMpD,WAAS,QAAA,UAAA,OAAA,OAAA;;iBAKL;WACN,UAAU,cAAc,OAAO;WAC/B;;;iBAUM,YAAY;WAClB,uBAAuB;WACvB,UAAU,MAAe;WACzB,SAAS,OAAO,OAAO,cAAc,OAAO,WAAW;WACvD,aAAa,cAAc;;;iBAIrB,QAAQ,mBAAmB,YAAY;WAC7C;;;iBAKM;;WAEN,QAAQ;;;;;;WAMR;;;;WAIA;;;YA4LC,KAAK,GAAG,KAAK,KAAK;EAC5B;aAAkB,OAAO;;EACzB;aAAiB,OAAO;;EACxB,MAAM;;;qBAUK,QAAS,GAAC,OAAS,MAAI,KAAK;;qBAG5B,OAAQ,GAAC,OAAS,MAAI,OAAO,OAAO,YAAY;;qBAIhD,OAAQ,gBAAc,OAAO,OAAO,YAAY;;qBAOhD,OAAQ,GAAG,GAAG,GAAG,GAAC,SACpB,GAAC,OACH,OAAO,MAAM,OAAO,OAAO,KAAK,GAAG,IAAI,GAAG,OAChD,OAAO,OAAO,GAAG,GAAG;;qBAmBV,OAAQ,GAAG,GAAG,YAAY,YAAU,SACtC,YAAY,IAAE,OAChB,QAAQ,eAAe,OAAO,OAAO,YAAY,IAAI,IAAI,QAC/D,OAAO,OAAO,KAAK,OAAO,IAAI,QAAQ,UAAU,IAAI,cAAc,gBAAgB,IAAI;;qBA8E5E,MAAO,WAAS,SAClB,QAAQ,OAChB,OAAO,OAAO,OAAO,QAAQ,SAAS,cAAc,gBAAgB"}
|
package/dist/internal/loop.js
CHANGED
|
@@ -2,7 +2,7 @@ import { ApprovalRequested, ModelOutput, ModelPart, ToolResult, ToolStarted } fr
|
|
|
2
2
|
import { collect } from "./stream.js";
|
|
3
3
|
import { annotateModelCall, toolOutput, toolSpan } from "./telemetry.js";
|
|
4
4
|
import { unansweredCalls } from "./history.js";
|
|
5
|
-
import { isDescribed,
|
|
5
|
+
import { isDescribed, parameterName } from "../ToolStatus.js";
|
|
6
6
|
import { use } from "./live.js";
|
|
7
7
|
import { empty, failed } from "./toolkit.js";
|
|
8
8
|
import { isTurnEnding, isUserMessage } from "./tools.js";
|
|
@@ -15,7 +15,6 @@ import * as Stream from "effect/Stream";
|
|
|
15
15
|
import * as Data from "effect/Data";
|
|
16
16
|
import * as Model from "effect/ai/Model";
|
|
17
17
|
import * as Prompt from "effect/ai/Prompt";
|
|
18
|
-
import * as SchemaAST from "effect/SchemaAST";
|
|
19
18
|
import * as LanguageModel from "effect/ai/LanguageModel";
|
|
20
19
|
import * as Clock from "effect/Clock";
|
|
21
20
|
//#region src/internal/loop.ts
|
|
@@ -30,19 +29,8 @@ const readHistory = (read) => "messages" in read ? {
|
|
|
30
29
|
messages: read,
|
|
31
30
|
throughSequence: void 0
|
|
32
31
|
};
|
|
33
|
-
const statusParameter = Schema.String.annotate({ description: parameterDescription });
|
|
34
|
-
const withStatusParameter = (tool) => {
|
|
35
|
-
const ast = tool.parametersSchema.ast;
|
|
36
|
-
if (!isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool;
|
|
37
|
-
if (ast.propertySignatures.some((property) => property.name === "description")) throw new Error(`${tool.name} has a parameter named ${parameterName} already, so it can't show a status: rename it.`);
|
|
38
|
-
const added = Schema.Struct({ [parameterName]: statusParameter }).ast.propertySignatures;
|
|
39
|
-
return tool.setParameters(Schema.make(new SchemaAST.Objects([...ast.propertySignatures, ...added], ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)), ast.annotations)));
|
|
40
|
-
};
|
|
41
32
|
const requestToolkit = (definitions) => ({
|
|
42
|
-
tools: Object.fromEntries(definitions.map((tool) =>
|
|
43
|
-
const requested = withStatusParameter(tool);
|
|
44
|
-
return [tool.name, requested.needsApproval === void 0 || requested.needsApproval === false ? requested : requested.setNeedsApproval(false)];
|
|
45
|
-
})),
|
|
33
|
+
tools: Object.fromEntries(definitions.map((tool) => [tool.name, tool.needsApproval === void 0 || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)])),
|
|
46
34
|
handle: () => Effect.succeed(Stream.empty)
|
|
47
35
|
});
|
|
48
36
|
const startsMessage = (part) => part.type === "text-start" || part.type === "tool-params-start" && part.name === "send_user_message";
|
|
@@ -58,21 +46,12 @@ const modelParts = (content) => content.flatMap((part) => Option.toArray(decodeP
|
|
|
58
46
|
const toolCalls = (parts) => parts.flatMap((part) => part.type === "tool-call" ? [part] : []);
|
|
59
47
|
const decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json));
|
|
60
48
|
const isDescribedCall = (tools, call) => tools.definitions.some((tool) => tool.name === call.name && isDescribed(tool));
|
|
61
|
-
const
|
|
62
|
-
const params = decodeParams(call.params);
|
|
63
|
-
if (Option.isNone(params) || !Object.hasOwn(params.value, "description")) return [void 0, call];
|
|
64
|
-
const { [parameterName]: written, ...rest } = params.value;
|
|
65
|
-
const status = Predicate.isString(written) ? written.trim() : "";
|
|
66
|
-
return [status === "" ? void 0 : status, {
|
|
67
|
-
...call,
|
|
68
|
-
params: rest
|
|
69
|
-
}];
|
|
70
|
-
};
|
|
49
|
+
const statusOf = (call) => decodeParams(call.params).pipe(Option.flatMapNullishOr((params) => params[parameterName]), Option.filter(Predicate.isString), Option.map((written) => written.trim()), Option.filter((status) => status !== ""), Option.getOrUndefined);
|
|
71
50
|
const runCall = Effect.fnUntraced(function* (tools, call) {
|
|
72
|
-
const
|
|
51
|
+
const status = isDescribedCall(tools, call) ? statusOf(call) : void 0;
|
|
73
52
|
const result = yield* Effect.gen(function* () {
|
|
74
53
|
if (status !== void 0) yield* use((live) => live.callStarted(call.id, status));
|
|
75
|
-
const result = yield* tools.run(
|
|
54
|
+
const result = yield* tools.run(call);
|
|
76
55
|
yield* toolOutput(result.result);
|
|
77
56
|
return result;
|
|
78
57
|
}).pipe(toolSpan(call), Effect.ensuring(use((live) => Effect.andThen(live.callEnded(call.id), call.name === "send_user_message" ? live.typing(false) : Effect.void))));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.js","names":["ToolStatus.parameterDescription","ToolStatus.isDescribed","ToolStatus.parameterName","Live.use","Streamed.collect","AssistantEvent.ModelPart","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.annotateModelCall"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as SchemaAST from \"effect/SchemaAST\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as ToolStatus from \"../ToolStatus.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport * as Streamed from \"./stream.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage, sendUserMessageName } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// A tool as `Tool.isUserDefined` narrows one. With `Tool.Any`, generateText's types ask for the services that encode\n// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.\ntype RequestTool = Tool.Tool<string, any, any>\n\n// Required in what the model reads, so it writes one with every call; a call without it still runs.\nconst statusParameter = Schema.String.annotate({ description: ToolStatus.parameterDescription })\n\n// What the model reads of a described tool: its parameters with the status one. Only the request has it; the call\n// runs without it (see `withoutStatus`). A record of no keys (a tool without parameters) becomes an object with it.\nconst withStatusParameter = (tool: Tool.Any): Tool.Any => {\n const ast = tool.parametersSchema.ast\n\n if (!ToolStatus.isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool\n\n if (ast.propertySignatures.some((property) => property.name === ToolStatus.parameterName)) {\n throw new Error(\n `${tool.name} has a parameter named ${ToolStatus.parameterName} already, so it can't show a status: rename it.`\n )\n }\n\n const added = Schema.Struct({ [ToolStatus.parameterName]: statusParameter }).ast.propertySignatures\n\n return tool.setParameters(\n Schema.make<Schema.Top>(\n new SchemaAST.Objects(\n [...ast.propertySignatures, ...added],\n ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)),\n ast.annotations\n )\n )\n )\n}\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => {\n const requested = withStatusParameter(tool)\n\n return [\n tool.name,\n requested.needsApproval === undefined || requested.needsApproval === false\n ? requested\n : requested.setNeedsApproval(false)\n ]\n })\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// The model starts a message to the user: a `send_user_message` call, or text of its own, which the reply pass turns\n// into one.\nconst startsMessage = (part: Response.AnyPart) =>\n part.type === \"text-start\" || (part.type === \"tool-params-start\" && part.name === sendUserMessageName)\n\n// Validation is deferred: a call to a tool that isn't in the toolkit (one in the history the turn no longer offers),\n// or with params that don't fit its schema, would otherwise fail the whole model call. The loop runs it instead, and\n// the model reads why it failed.\n// Streamed, so the chat can show the user that a message is on its way while the model writes it.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<ReadonlyArray<Response.AnyPart>, AiError.AiError, LanguageModel.LanguageModel> =>\n Stream.suspend(() =>\n LanguageModel.streamText({\n prompt,\n toolkit: requestToolkit(definitions),\n toolChoice,\n disableToolCallResolution: true,\n toolCallValidation: \"deferred\"\n })\n ).pipe(\n Stream.tap((part) => (startsMessage(part) ? Live.use((live) => live.typing(true)) : Effect.void)),\n Stream.runCollect,\n Effect.map(Streamed.collect)\n )\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AnyPart>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json))\n\nconst isDescribedCall = <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) =>\n tools.definitions.some((tool) => tool.name === call.name && ToolStatus.isDescribed(tool))\n\n// The status the model wrote for a described tool's call, and the call as the tool's handler gets it.\nconst splitStatus = (call: AssistantEvent.ToolCallPart): readonly [string | undefined, AssistantEvent.ToolCallPart] => {\n const params = decodeParams(call.params)\n\n if (Option.isNone(params) || !Object.hasOwn(params.value, ToolStatus.parameterName)) return [undefined, call]\n\n const { [ToolStatus.parameterName]: written, ...rest } = params.value\n const status = Predicate.isString(written) ? written.trim() : \"\"\n\n return [status === \"\" ? undefined : status, { ...call, params: rest }]\n}\n\n// A call's status shows when it starts; a message's typing stops once the message is stored.\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const [status, run] = isDescribedCall(tools, call) ? splitStatus(call) : [undefined, call]\n\n const result = yield* Effect.gen(function* () {\n if (status !== undefined) yield* Live.use((live) => live.callStarted(call.id, status))\n\n const result = yield* tools.run(run)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(\n Telemetry.toolSpan(call),\n Effect.ensuring(\n Live.use((live) =>\n Effect.andThen(live.callEnded(call.id), call.name === sendUserMessageName ? live.typing(false) : Effect.void)\n )\n )\n )\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n // Text that became no message, or a message that failed, stops the typing too.\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n ).pipe(Effect.ensuring(Live.use((live) => live.typing(false))))\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const content = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.annotateModelCall(instructions.attributes)\n )\n\n const metadata = content.find((part) => part.type === \"response-metadata\")\n const finish = content.find((part) => part.type === \"finish\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: finish?.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,EACtG,cAAc,OAAO,KACvB,CAAC;AAQD,MAAM,eAAe,SACnB,cAAc,OACV;CAAE,UAAU,KAAK;CAAU,iBAAiB,KAAK;AAAgB,IACjE;CAAE,UAAU;CAAM,iBAAiB,KAAA;AAAU;AAsCnD,MAAM,kBAAkB,OAAO,OAAO,SAAS,EAAE,aAAaA,qBAAgC,CAAC;AAI/F,MAAM,uBAAuB,SAA6B;CACxD,MAAM,MAAM,KAAK,iBAAiB;CAElC,IAAI,CAACC,YAAuB,IAAI,KAAK,CAAC,UAAU,UAAU,GAAG,GAAG,OAAO;CAEvE,IAAI,IAAI,mBAAmB,MAAM,aAAa,SAAS,SAAS,aAAwB,GACtF,MAAM,IAAI,MACR,GAAG,KAAK,KAAK,yBAAyBC,cAAyB,gDACjE;CAGF,MAAM,QAAQ,OAAO,OAAO,GAAGA,gBAA2B,gBAAgB,CAAC,CAAC,CAAC,IAAI;CAEjF,OAAO,KAAK,cACV,OAAO,KACL,IAAI,UAAU,QACZ,CAAC,GAAG,IAAI,oBAAoB,GAAG,KAAK,GACpC,IAAI,gBAAgB,QAAQ,UAAU,CAAC,UAAU,QAAQ,MAAM,IAAI,CAAC,GACpE,IAAI,WACN,CACF,CACF;AACF;AAIA,MAA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AU;GAAE;GAAM,UAAU;EAAM,EAAE,GAC/C,UACC,EAAE,QAAQ,cACT,MAAM;GACJ;GACA,MAAM,MAAM,WAAW,KAAK,WAAY,CAAC,UAAU,MAAM,KAAK,YAAY;GAC1E;EACF,CAAC,CACL,CACF,CACF;CACF,CAAC,CACH;AACF;;AAGA,MAAa,OACX,YAC2E;CAC3E,MAAM,WAAW,QAAQ,YAAY;CAGrC,MAAM,aAAa,OAAe,kBAChC,OAAO,OACL,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,OAAO,QAAQ;EAE5B,OAAO,YAAY,IAAI,CAAC,CAAC,SAAS,SAAS,gBAAgB,KAAK,KAAK,IAAI,KAAa;CACxF,CAAC,CACH;CAEF,OAAO,KAAK,IAAI,UACd,KAAK,UAAU,WAAW;EACxB,MAAM,QAAQ,OAAO,WAAW,KAAA,IAAY,QAAQ,QAAQ;EAE5D,IAAI,SAAS,UAAU,OAAO,KAAa;EAE3C,OAAO,OAAO,OAAO,UAAU,OAAO,OAAO,aAAa,IAAI,KAAK,KAAK;CAC1E,CAAC,CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"loop.js","names":["Live.use","Streamed.collect","AssistantEvent.ModelPart","ToolStatus.isDescribed","ToolStatus.parameterName","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.annotateModelCall"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as ToolStatus from \"../ToolStatus.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport * as Streamed from \"./stream.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage, sendUserMessageName } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// A tool as `Tool.isUserDefined` narrows one. With `Tool.Any`, generateText's types ask for the services that encode\n// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.\ntype RequestTool = Tool.Tool<string, any, any>\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => [\n tool.name,\n tool.needsApproval === undefined || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)\n ])\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// The model starts a message to the user: a `send_user_message` call, or text of its own, which the reply pass turns\n// into one.\nconst startsMessage = (part: Response.AnyPart) =>\n part.type === \"text-start\" || (part.type === \"tool-params-start\" && part.name === sendUserMessageName)\n\n// Validation is deferred: a call to a tool that isn't in the toolkit (one in the history the turn no longer offers),\n// or with params that don't fit its schema, would otherwise fail the whole model call. The loop runs it instead, and\n// the model reads why it failed.\n// Streamed, so the chat can show the user that a message is on its way while the model writes it.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<ReadonlyArray<Response.AnyPart>, AiError.AiError, LanguageModel.LanguageModel> =>\n Stream.suspend(() =>\n LanguageModel.streamText({\n prompt,\n toolkit: requestToolkit(definitions),\n toolChoice,\n disableToolCallResolution: true,\n toolCallValidation: \"deferred\"\n })\n ).pipe(\n Stream.tap((part) => (startsMessage(part) ? Live.use((live) => live.typing(true)) : Effect.void)),\n Stream.runCollect,\n Effect.map(Streamed.collect)\n )\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AnyPart>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.Json))\n\nconst isDescribedCall = <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) =>\n tools.definitions.some((tool) => tool.name === call.name && ToolStatus.isDescribed(tool))\n\nconst statusOf = (call: AssistantEvent.ToolCallPart): string | undefined =>\n decodeParams(call.params).pipe(\n Option.flatMapNullishOr((params) => params[ToolStatus.parameterName]),\n Option.filter(Predicate.isString),\n Option.map((written) => written.trim()),\n Option.filter((status) => status !== \"\"),\n Option.getOrUndefined\n )\n\n// A call's status shows when it starts; a message's typing stops once the message is stored.\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const status = isDescribedCall(tools, call) ? statusOf(call) : undefined\n\n const result = yield* Effect.gen(function* () {\n if (status !== undefined) yield* Live.use((live) => live.callStarted(call.id, status))\n\n const result = yield* tools.run(call)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(\n Telemetry.toolSpan(call),\n Effect.ensuring(\n Live.use((live) =>\n Effect.andThen(live.callEnded(call.id), call.name === sendUserMessageName ? live.typing(false) : Effect.void)\n )\n )\n )\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n // Text that became no message, or a message that failed, stops the typing too.\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n ).pipe(Effect.ensuring(Live.use((live) => live.typing(false))))\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const content = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.annotateModelCall(instructions.attributes)\n )\n\n const metadata = content.find((part) => part.type === \"response-metadata\")\n const finish = content.find((part) => part.type === \"finish\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: finish?.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,EACtG,cAAc,OAAO,KACvB,CAAC;AAQD,MAAM,eAAe,SACnB,cAAc,OACV;CAAE,UAAU,KAAK;CAAU,iBAAiB,KAAK;AAAgB,IACjE;CAAE,UAAU;CAAM,iBAAiB,KAAA;AAAU;AAuCnD,MAAM,kBA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AQ,YAAY;CAGrC,MAAM,aAAa,OAAe,kBAChC,OAAO,OACL,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,OAAO,QAAQ;EAE5B,OAAO,YAAY,IAAI,CAAC,CAAC,SAAS,SAAS,gBAAgB,KAAK,KAAK,IAAI,KAAa;CACxF,CAAC,CACH;CAEF,OAAO,KAAK,IAAI,UACd,KAAK,UAAU,WAAW;EACxB,MAAM,QAAQ,OAAO,WAAW,KAAA,IAAY,QAAQ,QAAQ;EAE5D,IAAI,SAAS,UAAU,OAAO,KAAa;EAE3C,OAAO,OAAO,OAAO,UAAU,OAAO,OAAO,aAAa,IAAI,KAAK,KAAK;CAC1E,CAAC,CACH;AACF"}
|
package/dist/internal/summary.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isLibraryEvent } from "../AssistantEvent.js";
|
|
1
|
+
import { isLibraryEvent, isTurnEnd } from "../AssistantEvent.js";
|
|
2
2
|
import { toJsonText } from "./schema.js";
|
|
3
3
|
import { fromEntries } from "./history.js";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
@@ -31,10 +31,29 @@ const runningCalls = (entries) => {
|
|
|
31
31
|
const answered = new Set(events.flatMap((event) => Predicate.isTagged(event, "ToolResult") ? [event.callId] : []));
|
|
32
32
|
return events.flatMap((event) => Predicate.isTagged(event, "ToolStarted") && !answered.has(event.callId) ? [event.tool] : []);
|
|
33
33
|
};
|
|
34
|
+
const turnStart = (entries) => {
|
|
35
|
+
const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, "TurnStarted"));
|
|
36
|
+
return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => isTurnEnd(event)) + 1;
|
|
37
|
+
};
|
|
34
38
|
/** @internal */
|
|
35
|
-
const transcript = (entries) => {
|
|
36
|
-
const
|
|
37
|
-
|
|
39
|
+
const transcript = (entries, shown) => {
|
|
40
|
+
const start = turnStart(entries);
|
|
41
|
+
const all = fromEntries(entries);
|
|
42
|
+
const earlierCount = Math.min(fromEntries(entries.slice(0, start)).length, all.length);
|
|
43
|
+
const earlier = all.slice(0, earlierCount).slice(-4).flatMap(lines);
|
|
44
|
+
const now = all.slice(earlierCount).slice(-12).flatMap(lines);
|
|
45
|
+
const running = runningCalls(entries.slice(start));
|
|
46
|
+
return [
|
|
47
|
+
...earlier.length === 0 ? [] : [
|
|
48
|
+
"Earlier in the chat:",
|
|
49
|
+
...earlier,
|
|
50
|
+
""
|
|
51
|
+
],
|
|
52
|
+
"This turn:",
|
|
53
|
+
...now,
|
|
54
|
+
...running.length === 0 ? [] : [`Running now: ${running.join(", ")}`],
|
|
55
|
+
...shown.length === 0 ? [] : ["", `Shown so far: ${shown.map((line) => `"${line}"`).join(", ")}`]
|
|
56
|
+
].join("\n");
|
|
38
57
|
};
|
|
39
58
|
const lastSequence = (entries) => entries.at(-1)?.sequence ?? 0;
|
|
40
59
|
const isWork = (event) => Predicate.isTagged(event, "TurnStarted") || Predicate.isTagged(event, "ModelOutput") || Predicate.isTagged(event, "ToolStarted");
|
|
@@ -55,7 +74,7 @@ const run = Effect.fnUntraced(function* (summary, live, chat) {
|
|
|
55
74
|
const through = lastSequence(entries);
|
|
56
75
|
const waited = (yield* Clock.currentTimeMillis) - current.shownAt;
|
|
57
76
|
if (current.typing || current.running.length > 0 || waited < (current.shown === void 0 ? after : staleAfter) || through === (yield* Ref.get(summarized)) || !workingSinceMessage(entries)) return;
|
|
58
|
-
const written = yield* summary.summarize(transcript(entries));
|
|
77
|
+
const written = yield* summary.summarize(transcript(entries, current.lines));
|
|
59
78
|
yield* Ref.set(summarized, through);
|
|
60
79
|
if (Option.isSome(written)) yield* live.summarized(written.value);
|
|
61
80
|
}).pipe(Effect.repeat(Schedule.spaced(Math.max(MIN_CHECK_MILLIS, Math.min(MAX_CHECK_MILLIS, after / 2, staleAfter / 2)))));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summary.js","names":["AssistantEvent.isLibraryEvent","History.fromEntries"],"sources":["../../src/internal/summary.ts"],"sourcesContent":["import type * as Prompt from \"effect/ai/Prompt\"\nimport * as Clock from \"effect/Clock\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Ref from \"effect/Ref\"\nimport * as Schedule from \"effect/Schedule\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as StatusSummary from \"../StatusSummary.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport { toJsonText } from \"./schema.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst MAX_LINE = 300\n\nconst MAX_MESSAGES = 12\n\nconst clip = (text: string) => (text.length > MAX_LINE ? `${text.slice(0, MAX_LINE)}…` : text)\n\nconst assistantLines = (part: Prompt.AssistantMessagePart): ReadonlyArray<string> => {\n switch (part.type) {\n case \"text\":\n return [`Assistant: ${clip(part.text)}`]\n case \"tool-call\":\n return [`Assistant runs ${part.name} ${clip(toJsonText(part.params))}`]\n default:\n return []\n }\n}\n\nconst lines = (message: Prompt.Message): ReadonlyArray<string> => {\n switch (message.role) {\n case \"system\":\n return []\n case \"user\":\n return message.content.flatMap((part) => (part.type === \"text\" ? [`User: ${clip(part.text)}`] : []))\n case \"assistant\":\n return message.content.flatMap(assistantLines)\n case \"tool\":\n return message.content.flatMap((part) =>\n part.type === \"tool-result\"\n ? [`Result of ${part.name}: ${clip(Predicate.isString(part.result) ? part.result : toJsonText(part.result))}`]\n : []\n )\n }\n}\n\n// The calls started and not answered yet: what the assistant is doing right now.\nconst runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const events = entries.flatMap(({ event }) => (AssistantEvent.isLibraryEvent(event) ? [event] : []))\n const answered = new Set(events.flatMap((event) => (Predicate.isTagged(event, \"ToolResult\") ? [event.callId] : [])))\n\n return events.flatMap((event) =>\n Predicate.isTagged(event, \"ToolStarted\") && !answered.has(event.callId) ? [event.tool] : []\n )\n}\n\n// The
|
|
1
|
+
{"version":3,"file":"summary.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isTurnEnd","History.fromEntries"],"sources":["../../src/internal/summary.ts"],"sourcesContent":["import type * as Prompt from \"effect/ai/Prompt\"\nimport * as Clock from \"effect/Clock\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Ref from \"effect/Ref\"\nimport * as Schedule from \"effect/Schedule\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as StatusSummary from \"../StatusSummary.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport { toJsonText } from \"./schema.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst MAX_LINE = 300\n\nconst MAX_MESSAGES = 12\n\nconst clip = (text: string) => (text.length > MAX_LINE ? `${text.slice(0, MAX_LINE)}…` : text)\n\nconst assistantLines = (part: Prompt.AssistantMessagePart): ReadonlyArray<string> => {\n switch (part.type) {\n case \"text\":\n return [`Assistant: ${clip(part.text)}`]\n case \"tool-call\":\n return [`Assistant runs ${part.name} ${clip(toJsonText(part.params))}`]\n default:\n return []\n }\n}\n\nconst lines = (message: Prompt.Message): ReadonlyArray<string> => {\n switch (message.role) {\n case \"system\":\n return []\n case \"user\":\n return message.content.flatMap((part) => (part.type === \"text\" ? [`User: ${clip(part.text)}`] : []))\n case \"assistant\":\n return message.content.flatMap(assistantLines)\n case \"tool\":\n return message.content.flatMap((part) =>\n part.type === \"tool-result\"\n ? [`Result of ${part.name}: ${clip(Predicate.isString(part.result) ? part.result : toJsonText(part.result))}`]\n : []\n )\n }\n}\n\n// The calls started and not answered yet: what the assistant is doing right now.\nconst runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const events = entries.flatMap(({ event }) => (AssistantEvent.isLibraryEvent(event) ? [event] : []))\n const answered = new Set(events.flatMap((event) => (Predicate.isTagged(event, \"ToolResult\") ? [event.callId] : [])))\n\n return events.flatMap((event) =>\n Predicate.isTagged(event, \"ToolStarted\") && !answered.has(event.callId) ? [event.tool] : []\n )\n}\n\nconst MAX_EARLIER = 4\n\n// The turn's input follows the end of the turn before it.\nconst turnStart = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, \"TurnStarted\"))\n\n return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => AssistantEvent.isTurnEnd(event)) + 1\n}\n\n// This turn's messages as plain lines, after a few earlier ones that are only context, with what still runs and the\n// lines the user saw: the summary's model gets no tools, so no tool call parts.\n/** @internal */\nexport const transcript = (entries: ReadonlyArray<AssistantEvent.AnyEntry>, shown: ReadonlyArray<string>): string => {\n const start = turnStart(entries)\n const all = History.fromEntries(entries)\n const earlierCount = Math.min(History.fromEntries(entries.slice(0, start)).length, all.length)\n const earlier = all.slice(0, earlierCount).slice(-MAX_EARLIER).flatMap(lines)\n const now = all.slice(earlierCount).slice(-MAX_MESSAGES).flatMap(lines)\n const running = runningCalls(entries.slice(start))\n\n return [\n ...(earlier.length === 0 ? [] : [\"Earlier in the chat:\", ...earlier, \"\"]),\n \"This turn:\",\n ...now,\n ...(running.length === 0 ? [] : [`Running now: ${running.join(\", \")}`]),\n ...(shown.length === 0 ? [] : [\"\", `Shown so far: ${shown.map((line) => `\"${line}\"`).join(\", \")}`])\n ].join(\"\\n\")\n}\n\nconst lastSequence = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => entries.at(-1)?.sequence ?? 0\n\nconst isWork = (event: { readonly _tag: string }) =>\n Predicate.isTagged(event, \"TurnStarted\") ||\n Predicate.isTagged(event, \"ModelOutput\") ||\n Predicate.isTagged(event, \"ToolStarted\")\n\n// After a message the user is up to date: a summary shows again only for work the model started since, not for what a\n// handler does after its reply (a compaction, say).\nconst workingSinceMessage = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const message = entries.findLastIndex(({ event }) => Predicate.isTagged(event, \"AssistantMessage\"))\n\n return message === -1 || entries.slice(message + 1).some(({ event }) => isWork(event))\n}\n\nconst MAX_CHECK_MILLIS = 500\n\nconst MIN_CHECK_MILLIS = 100\n\n// A line for a turn that shows nothing for a while, or whose line hasn't changed for longer: never while a described\n// call says what runs, or the model writes to the user, and only after something new happened.\n/** @internal */\nexport const run = Effect.fnUntraced(function* (\n summary: StatusSummary.StatusSummary[\"Service\"],\n live: Live.Live,\n chat: Writer\n) {\n const summarized = yield* Ref.make(-1)\n const after = Duration.toMillis(summary.after)\n const staleAfter = Duration.toMillis(summary.staleAfter)\n\n const tick = Effect.gen(function* () {\n const current = yield* live.state\n const entries = yield* chat.entries\n const through = lastSequence(entries)\n const waited = (yield* Clock.currentTimeMillis) - current.shownAt\n\n if (\n current.typing ||\n current.running.length > 0 ||\n waited < (current.shown === undefined ? after : staleAfter) ||\n through === (yield* Ref.get(summarized)) ||\n !workingSinceMessage(entries)\n ) {\n return\n }\n\n const written = yield* summary.summarize(transcript(entries, current.lines))\n\n yield* Ref.set(summarized, through)\n\n if (Option.isSome(written)) yield* live.summarized(written.value)\n })\n\n // Often enough that a line is filled or refreshed close to when it's due.\n yield* tick.pipe(\n Effect.repeat(Schedule.spaced(Math.max(MIN_CHECK_MILLIS, Math.min(MAX_CHECK_MILLIS, after / 2, staleAfter / 2))))\n )\n})\n"],"mappings":";;;;;;;;;;;AAeA,MAAM,WAAW;AAIjB,MAAM,QAAQ,SAAkB,KAAK,SAAS,WAAW,GAAG,KAAK,MAAM,GAAG,QAAQ,EAAE,KAAK;AAEzF,MAAM,kBAAkB,SAA6D;CACnF,QAAQ,KAAK,MAAb;EACE,KAAK,QACH,OAAO,CAAC,cAAc,KAAK,KAAK,IAAI,GAAG;EACzC,KAAK,aACH,OAAO,CAAC,kBAAkB,KAAK,KAAK,GAAG,KAAK,WAAW,KAAK,MAAM,CAAC,GAAG;EACxE,SACE,OAAO,CAAC;CACZ;AACF;AAE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O,IAAI,aAAa;EACnC,MAAM,UAAU,OAAO,KAAK;EAC5B,MAAM,UAAU,OAAO,KAAK;EAC5B,MAAM,UAAU,aAAa,OAAO;EACpC,MAAM,UAAU,OAAO,MAAM,qBAAqB,QAAQ;EAE1D,IACE,QAAQ,UACR,QAAQ,QAAQ,SAAS,KACzB,UAAU,QAAQ,UAAU,KAAA,IAAY,QAAQ,eAChD,aAAa,OAAO,IAAI,IAAI,UAAU,MACtC,CAAC,oBAAoB,OAAO,GAE5B;EAGF,MAAM,UAAU,OAAO,QAAQ,UAAU,WAAW,SAAS,QAAQ,KAAK,CAAC;EAE3E,OAAO,IAAI,IAAI,YAAY,OAAO;EAElC,IAAI,OAAO,OAAO,OAAO,GAAG,OAAO,KAAK,WAAW,QAAQ,KAAK;CAClE,CAGU,CAAC,CAAC,KACV,OAAO,OAAO,SAAS,OAAO,KAAK,IAAI,kBAAkB,KAAK,IAAI,kBAAkB,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAClH;AACF,CAAC"}
|
package/package.json
CHANGED
package/src/StatusSummary.ts
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
* Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary
|
|
8
8
|
* is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.
|
|
9
9
|
* It never replaces the status of a described call that still runs, never shows while the model
|
|
10
|
-
* writes to the user, and never after the turn's message unless the model starts new work.
|
|
11
|
-
*
|
|
10
|
+
* writes to the user, and never after the turn's message unless the model starts new work. The
|
|
11
|
+
* model reads this turn, after a few earlier messages as context, and the lines the turn showed so
|
|
12
|
+
* far, and answers `-` when nothing new happened since: then the line stays as it is.
|
|
12
13
|
*
|
|
13
14
|
* @since 0.13.0
|
|
14
15
|
*/
|
|
@@ -27,7 +28,7 @@ import * as Option from "effect/Option"
|
|
|
27
28
|
* @since 0.13.0
|
|
28
29
|
*/
|
|
29
30
|
export const defaultInstructions =
|
|
30
|
-
'You write the status line a chat shows while an assistant works for its user. Read
|
|
31
|
+
'You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like "Comparing last year\'s budgets". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.'
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Represents the options accepted by {@link layer}.
|
|
@@ -56,8 +57,9 @@ export class StatusSummary extends Context.Service<
|
|
|
56
57
|
readonly after: Duration.Duration
|
|
57
58
|
readonly staleAfter: Duration.Duration
|
|
58
59
|
/**
|
|
59
|
-
* Returns the status for a turn, from its transcript:
|
|
60
|
-
*
|
|
60
|
+
* Returns the status for a turn, from its transcript: this turn after a few earlier messages,
|
|
61
|
+
* what still runs, and the lines shown so far. `None` when the model has nothing new, wrote
|
|
62
|
+
* nothing usable, or its call failed.
|
|
61
63
|
*/
|
|
62
64
|
readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>
|
|
63
65
|
}
|
|
@@ -71,7 +73,7 @@ const clean = (text: string): Option.Option<string> => {
|
|
|
71
73
|
.replace(/[.…]+$/, "")
|
|
72
74
|
.trim()
|
|
73
75
|
|
|
74
|
-
return line === "" || line.length > MAX_LENGTH ? Option.none() : Option.some(line)
|
|
76
|
+
return line === "" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line)
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
/**
|
package/src/ToolStatus.ts
CHANGED
|
@@ -2,25 +2,18 @@
|
|
|
2
2
|
* This module provides status labels for tool calls: while a call runs, the chat shows the user a
|
|
3
3
|
* short line about what the assistant is doing, like "Checking last summer's campaign budget".
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* from other systems, orders, specialists.
|
|
7
|
-
*
|
|
8
|
-
* description as a `ChatSignal.Status`; it shows until the next status, so it stays
|
|
9
|
-
*
|
|
10
|
-
* them.
|
|
5
|
+
* Give the tools that take a while or act on something a status field with {@link withStatus}:
|
|
6
|
+
* workflows, reports from other systems, orders, specialists. Their parameters get a required
|
|
7
|
+
* `description` field that the model fills in the chat's language. When the call starts, the chat
|
|
8
|
+
* sends the description as a `ChatSignal.Status`; it shows until the next status, so it stays
|
|
9
|
+
* after the call ends. The call runs unchanged: the handler gets `description` with its other
|
|
10
|
+
* parameters. Quick reads need no label: the chat shows its generic working state for them.
|
|
11
11
|
*
|
|
12
12
|
* @since 0.13.0
|
|
13
13
|
*/
|
|
14
14
|
import type * as Tool from "effect/ai/Tool"
|
|
15
|
-
import * as
|
|
16
|
-
import
|
|
17
|
-
|
|
18
|
-
type Schemas = {
|
|
19
|
-
readonly parameters: Schema.Constraint
|
|
20
|
-
readonly success: Schema.Constraint
|
|
21
|
-
readonly failure: Schema.Constraint
|
|
22
|
-
readonly failureMode: Tool.FailureMode
|
|
23
|
-
}
|
|
15
|
+
import * as Schema from "effect/Schema"
|
|
16
|
+
import * as SchemaAST from "effect/SchemaAST"
|
|
24
17
|
|
|
25
18
|
/**
|
|
26
19
|
* The name of the parameter the model fills with a call's status.
|
|
@@ -39,53 +32,85 @@ export const parameterName = "description"
|
|
|
39
32
|
export const parameterDescription =
|
|
40
33
|
"Shown to the user while this runs: 3 to 8 plain words in the user's language about what you're doing, like \"Checking last summer's campaign budget\". No tool names, ids or code."
|
|
41
34
|
|
|
35
|
+
const statusFieldKey = "effect-assistant/ToolStatus/statusField"
|
|
36
|
+
|
|
42
37
|
/**
|
|
43
|
-
*
|
|
38
|
+
* The schema of the status field {@link withStatus} adds.
|
|
44
39
|
*
|
|
45
|
-
* @category
|
|
46
|
-
* @since 0.
|
|
40
|
+
* @category schemas
|
|
41
|
+
* @since 0.16.0
|
|
47
42
|
*/
|
|
48
|
-
export const
|
|
49
|
-
defaultValue: () => false
|
|
50
|
-
})
|
|
43
|
+
export const StatusField = Schema.String.annotate({ description: parameterDescription, [statusFieldKey]: true })
|
|
51
44
|
|
|
52
45
|
/**
|
|
53
|
-
*
|
|
46
|
+
* The parameter fields a struct can't have to take a status field: a `description` field of its own.
|
|
47
|
+
*
|
|
48
|
+
* @category models
|
|
49
|
+
* @since 0.16.0
|
|
50
|
+
*/
|
|
51
|
+
export type WithoutStatus<Fields extends Schema.Struct.Fields> = typeof parameterName extends keyof Fields
|
|
52
|
+
? "Error: these parameters have a description field already, so they can't take a status: rename it"
|
|
53
|
+
: unknown
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Adds the status field to a tool's parameters, so its calls show a status while they run.
|
|
54
57
|
*
|
|
55
58
|
* **Details**
|
|
56
59
|
*
|
|
57
|
-
* The
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
60
|
+
* The struct gets a required `description` field ({@link StatusField}), and keeps its own
|
|
61
|
+
* annotations and checks. Required, because models leave optional parameters out. The chat shows
|
|
62
|
+
* what the model writes there while the call runs; a call with a blank one runs with no status,
|
|
63
|
+
* and a call without it fails validation like a call without any other required parameter. The
|
|
64
|
+
* handler gets `description` like any other parameter. A struct that has a `description` field
|
|
65
|
+
* already is a type error. A tool without parameters takes `Schema.Struct({})`.
|
|
61
66
|
*
|
|
62
|
-
* **Example** (A
|
|
67
|
+
* **Example** (A tool whose calls show a status)
|
|
63
68
|
*
|
|
64
69
|
* ```ts
|
|
65
70
|
* import { ToolStatus } from "effect-assistant"
|
|
66
71
|
* import * as Tool from "effect/ai/Tool"
|
|
67
72
|
* import * as Schema from "effect/Schema"
|
|
68
73
|
*
|
|
69
|
-
* export const
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* })
|
|
75
|
-
* )
|
|
74
|
+
* export const ArchiveNote = Tool.make("archive_note", {
|
|
75
|
+
* description: "Archives a note and the files attached to it.",
|
|
76
|
+
* parameters: Schema.Struct({ noteId: Schema.String }).pipe(ToolStatus.withStatus),
|
|
77
|
+
* success: Schema.Struct({ archived: Schema.Boolean })
|
|
78
|
+
* })
|
|
76
79
|
* ```
|
|
77
80
|
*
|
|
78
|
-
* @category
|
|
79
|
-
* @since 0.
|
|
81
|
+
* @category combinators
|
|
82
|
+
* @since 0.16.0
|
|
80
83
|
*/
|
|
81
|
-
export const
|
|
82
|
-
|
|
83
|
-
)
|
|
84
|
+
export const withStatus = <Fields extends Schema.Struct.Fields>(
|
|
85
|
+
self: Schema.Struct<Fields> & WithoutStatus<Fields>
|
|
86
|
+
) => {
|
|
87
|
+
const struct = self.mapFields((fields) => ({ ...fields, [parameterName]: StatusField }))
|
|
88
|
+
|
|
89
|
+
return struct.rebuild(
|
|
90
|
+
new SchemaAST.Objects(
|
|
91
|
+
struct.ast.propertySignatures,
|
|
92
|
+
struct.ast.indexSignatures,
|
|
93
|
+
self.ast.annotations,
|
|
94
|
+
self.ast.checks
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
}
|
|
84
98
|
|
|
85
99
|
/**
|
|
86
|
-
* Returns whether a tool's calls show a status
|
|
100
|
+
* Returns whether a tool's calls show a status: its parameters have the field {@link withStatus}
|
|
101
|
+
* adds. A `description` parameter of its own doesn't count.
|
|
87
102
|
*
|
|
88
103
|
* @category guards
|
|
89
104
|
* @since 0.13.0
|
|
90
105
|
*/
|
|
91
|
-
export const isDescribed = (tool: Tool.Any): boolean =>
|
|
106
|
+
export const isDescribed = (tool: Tool.Any): boolean => {
|
|
107
|
+
const ast = tool.parametersSchema.ast
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
SchemaAST.isObjects(ast) &&
|
|
111
|
+
ast.propertySignatures.some(
|
|
112
|
+
(property) =>
|
|
113
|
+
property.name === parameterName && SchemaAST.resolveAt<boolean>(statusFieldKey)(property.type) === true
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
}
|
package/src/internal/live.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface State {
|
|
|
18
18
|
readonly shown: string | undefined
|
|
19
19
|
/** When the line last changed, or the turn started, in epoch milliseconds. */
|
|
20
20
|
readonly shownAt: number
|
|
21
|
+
/** Every line the turn showed, oldest first. */
|
|
22
|
+
readonly lines: ReadonlyArray<string>
|
|
21
23
|
readonly typing: boolean
|
|
22
24
|
}
|
|
23
25
|
|
|
@@ -54,6 +56,7 @@ export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatS
|
|
|
54
56
|
running: [],
|
|
55
57
|
shown: undefined,
|
|
56
58
|
shownAt: yield* Clock.currentTimeMillis,
|
|
59
|
+
lines: [],
|
|
57
60
|
typing: false
|
|
58
61
|
})
|
|
59
62
|
|
|
@@ -77,7 +80,7 @@ export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatS
|
|
|
77
80
|
const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>
|
|
78
81
|
current.shown === text
|
|
79
82
|
? [[], current]
|
|
80
|
-
: [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]
|
|
83
|
+
: [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now, lines: [...current.lines, text] }]
|
|
81
84
|
|
|
82
85
|
const live: Live = {
|
|
83
86
|
callStarted: (callId, text) =>
|
package/src/internal/loop.ts
CHANGED
|
@@ -12,7 +12,6 @@ import * as Effect from "effect/Effect"
|
|
|
12
12
|
import * as Option from "effect/Option"
|
|
13
13
|
import * as Predicate from "effect/Predicate"
|
|
14
14
|
import * as Schema from "effect/Schema"
|
|
15
|
-
import * as SchemaAST from "effect/SchemaAST"
|
|
16
15
|
import * as Stream from "effect/Stream"
|
|
17
16
|
import * as AssistantEvent from "../AssistantEvent.ts"
|
|
18
17
|
import type * as Instructions from "../Instructions.ts"
|
|
@@ -91,49 +90,14 @@ export interface StepResult {
|
|
|
91
90
|
// every tool's params, `unknown` for a schema of unknown type, though deferred validation encodes none.
|
|
92
91
|
type RequestTool = Tool.Tool<string, any, any>
|
|
93
92
|
|
|
94
|
-
// Required in what the model reads, so it writes one with every call; a call without it still runs.
|
|
95
|
-
const statusParameter = Schema.String.annotate({ description: ToolStatus.parameterDescription })
|
|
96
|
-
|
|
97
|
-
// What the model reads of a described tool: its parameters with the status one. Only the request has it; the call
|
|
98
|
-
// runs without it (see `withoutStatus`). A record of no keys (a tool without parameters) becomes an object with it.
|
|
99
|
-
const withStatusParameter = (tool: Tool.Any): Tool.Any => {
|
|
100
|
-
const ast = tool.parametersSchema.ast
|
|
101
|
-
|
|
102
|
-
if (!ToolStatus.isDescribed(tool) || !SchemaAST.isObjects(ast)) return tool
|
|
103
|
-
|
|
104
|
-
if (ast.propertySignatures.some((property) => property.name === ToolStatus.parameterName)) {
|
|
105
|
-
throw new Error(
|
|
106
|
-
`${tool.name} has a parameter named ${ToolStatus.parameterName} already, so it can't show a status: rename it.`
|
|
107
|
-
)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const added = Schema.Struct({ [ToolStatus.parameterName]: statusParameter }).ast.propertySignatures
|
|
111
|
-
|
|
112
|
-
return tool.setParameters(
|
|
113
|
-
Schema.make<Schema.Top>(
|
|
114
|
-
new SchemaAST.Objects(
|
|
115
|
-
[...ast.propertySignatures, ...added],
|
|
116
|
-
ast.indexSignatures.filter((index) => !SchemaAST.isNever(index.type)),
|
|
117
|
-
ast.annotations
|
|
118
|
-
)
|
|
119
|
-
)
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
93
|
// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which
|
|
124
94
|
// calls wait for approval: the model call would ask for approvals of its own.
|
|
125
95
|
const requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, RequestTool>> => ({
|
|
126
96
|
tools: Object.fromEntries(
|
|
127
|
-
definitions.map((tool) =>
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
tool.name,
|
|
132
|
-
requested.needsApproval === undefined || requested.needsApproval === false
|
|
133
|
-
? requested
|
|
134
|
-
: requested.setNeedsApproval(false)
|
|
135
|
-
]
|
|
136
|
-
})
|
|
97
|
+
definitions.map((tool) => [
|
|
98
|
+
tool.name,
|
|
99
|
+
tool.needsApproval === undefined || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)
|
|
100
|
+
])
|
|
137
101
|
),
|
|
138
102
|
handle: () => Effect.succeed(Stream.empty)
|
|
139
103
|
})
|
|
@@ -181,26 +145,23 @@ const decodeParams = Schema.decodeUnknownOption(Schema.Record(Schema.String, Sch
|
|
|
181
145
|
const isDescribedCall = <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) =>
|
|
182
146
|
tools.definitions.some((tool) => tool.name === call.name && ToolStatus.isDescribed(tool))
|
|
183
147
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
return [status === "" ? undefined : status, { ...call, params: rest }]
|
|
194
|
-
}
|
|
148
|
+
const statusOf = (call: AssistantEvent.ToolCallPart): string | undefined =>
|
|
149
|
+
decodeParams(call.params).pipe(
|
|
150
|
+
Option.flatMapNullishOr((params) => params[ToolStatus.parameterName]),
|
|
151
|
+
Option.filter(Predicate.isString),
|
|
152
|
+
Option.map((written) => written.trim()),
|
|
153
|
+
Option.filter((status) => status !== ""),
|
|
154
|
+
Option.getOrUndefined
|
|
155
|
+
)
|
|
195
156
|
|
|
196
157
|
// A call's status shows when it starts; a message's typing stops once the message is stored.
|
|
197
158
|
const runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {
|
|
198
|
-
const
|
|
159
|
+
const status = isDescribedCall(tools, call) ? statusOf(call) : undefined
|
|
199
160
|
|
|
200
161
|
const result = yield* Effect.gen(function* () {
|
|
201
162
|
if (status !== undefined) yield* Live.use((live) => live.callStarted(call.id, status))
|
|
202
163
|
|
|
203
|
-
const result = yield* tools.run(
|
|
164
|
+
const result = yield* tools.run(call)
|
|
204
165
|
|
|
205
166
|
yield* Telemetry.toolOutput(result.result)
|
|
206
167
|
|
package/src/internal/summary.ts
CHANGED
|
@@ -57,13 +57,33 @@ const runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
|
57
57
|
)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
60
|
+
const MAX_EARLIER = 4
|
|
61
|
+
|
|
62
|
+
// The turn's input follows the end of the turn before it.
|
|
63
|
+
const turnStart = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
64
|
+
const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, "TurnStarted"))
|
|
65
65
|
|
|
66
|
-
return
|
|
66
|
+
return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => AssistantEvent.isTurnEnd(event)) + 1
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// This turn's messages as plain lines, after a few earlier ones that are only context, with what still runs and the
|
|
70
|
+
// lines the user saw: the summary's model gets no tools, so no tool call parts.
|
|
71
|
+
/** @internal */
|
|
72
|
+
export const transcript = (entries: ReadonlyArray<AssistantEvent.AnyEntry>, shown: ReadonlyArray<string>): string => {
|
|
73
|
+
const start = turnStart(entries)
|
|
74
|
+
const all = History.fromEntries(entries)
|
|
75
|
+
const earlierCount = Math.min(History.fromEntries(entries.slice(0, start)).length, all.length)
|
|
76
|
+
const earlier = all.slice(0, earlierCount).slice(-MAX_EARLIER).flatMap(lines)
|
|
77
|
+
const now = all.slice(earlierCount).slice(-MAX_MESSAGES).flatMap(lines)
|
|
78
|
+
const running = runningCalls(entries.slice(start))
|
|
79
|
+
|
|
80
|
+
return [
|
|
81
|
+
...(earlier.length === 0 ? [] : ["Earlier in the chat:", ...earlier, ""]),
|
|
82
|
+
"This turn:",
|
|
83
|
+
...now,
|
|
84
|
+
...(running.length === 0 ? [] : [`Running now: ${running.join(", ")}`]),
|
|
85
|
+
...(shown.length === 0 ? [] : ["", `Shown so far: ${shown.map((line) => `"${line}"`).join(", ")}`])
|
|
86
|
+
].join("\n")
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
const lastSequence = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => entries.at(-1)?.sequence ?? 0
|
|
@@ -113,7 +133,7 @@ export const run = Effect.fnUntraced(function* (
|
|
|
113
133
|
return
|
|
114
134
|
}
|
|
115
135
|
|
|
116
|
-
const written = yield* summary.summarize(transcript(entries))
|
|
136
|
+
const written = yield* summary.summarize(transcript(entries, current.lines))
|
|
117
137
|
|
|
118
138
|
yield* Ref.set(summarized, through)
|
|
119
139
|
|