@tano.page/protocol 0.15.0 → 0.16.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/manifest.schema.json +59 -30
- package/package.json +13 -3
- package/src/domain/agent-identities.ts +110 -0
- package/src/domain/assistant-completion.ts +61 -0
- package/src/domain/assistant-engine.ts +614 -0
- package/src/domain/assistant-execution.ts +43 -0
- package/src/domain/assistant-language.ts +112 -0
- package/src/domain/assistant-prompts.ts +70 -0
- package/src/domain/assistant-structures.ts +179 -0
- package/src/domain/assistant-task-prompts.json +39 -0
- package/src/domain/assistant-tasks.ts +114 -0
- package/src/domain/locales/de-DE.json +129 -0
- package/src/domain/locales/en-US.json +129 -0
- package/src/domain/locales/es-ES.json +129 -0
- package/src/domain/locales/ja-JP.json +129 -0
- package/src/domain/locales/zh-CN.json +129 -0
- package/src/domain/locales/zh-TW.json +129 -0
- package/src/domain/plugin-data.ts +175 -0
- package/src/domain/plugin-sdk.ts +300 -0
- package/src/objects.ts +3 -3
package/manifest.schema.json
CHANGED
|
@@ -17,6 +17,18 @@
|
|
|
17
17
|
"type": "string",
|
|
18
18
|
"pattern": "^[a-z][a-z0-9.-]{0,79}$"
|
|
19
19
|
},
|
|
20
|
+
"assistantToolName": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"pattern": "^[a-z][a-z0-9_-]{0,47}$"
|
|
23
|
+
},
|
|
24
|
+
"assistantContextId": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"pattern": "^[a-z][a-z0-9_-]{0,31}$"
|
|
27
|
+
},
|
|
28
|
+
"serviceName": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"pattern": "^[a-z][a-z0-9_.-]{0,47}$"
|
|
31
|
+
},
|
|
20
32
|
"semver": {
|
|
21
33
|
"type": "string",
|
|
22
34
|
"pattern": "^\\d+\\.\\d+\\.\\d+$"
|
|
@@ -32,7 +44,7 @@
|
|
|
32
44
|
"tano": {
|
|
33
45
|
"type": "string",
|
|
34
46
|
"pattern": "^>=\\d+\\.\\d+\\.\\d+$",
|
|
35
|
-
"description": "Minimum supported Tano Core semver, e.g. \">=0.
|
|
47
|
+
"description": "Minimum supported Tano Core semver, e.g. \">=0.15.0\"."
|
|
36
48
|
}
|
|
37
49
|
}
|
|
38
50
|
},
|
|
@@ -102,10 +114,11 @@
|
|
|
102
114
|
"id": { "$ref": "#/$defs/pluginKey" },
|
|
103
115
|
"label": { "type": "string", "minLength": 1, "maxLength": 80 },
|
|
104
116
|
"command": { "$ref": "#/$defs/pluginKey" },
|
|
105
|
-
"description": { "type": "string", "maxLength":
|
|
117
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 120 },
|
|
106
118
|
"keywords": {
|
|
107
119
|
"type": "array",
|
|
108
|
-
"maxItems":
|
|
120
|
+
"maxItems": 10,
|
|
121
|
+
"uniqueItems": true,
|
|
109
122
|
"items": { "type": "string", "minLength": 1, "maxLength": 40 }
|
|
110
123
|
}
|
|
111
124
|
}
|
|
@@ -129,12 +142,13 @@
|
|
|
129
142
|
"languages": {
|
|
130
143
|
"type": "array",
|
|
131
144
|
"minItems": 1,
|
|
132
|
-
"maxItems":
|
|
133
|
-
"
|
|
145
|
+
"maxItems": 8,
|
|
146
|
+
"uniqueItems": true,
|
|
147
|
+
"items": { "type": "string", "pattern": "^[a-z][a-z0-9-]{0,31}$" }
|
|
134
148
|
},
|
|
135
149
|
"view": { "$ref": "#/$defs/pluginKey" },
|
|
136
150
|
"label": { "type": "string", "minLength": 1, "maxLength": 80 },
|
|
137
|
-
"height": { "type": "integer", "minimum":
|
|
151
|
+
"height": { "type": "integer", "minimum": 120, "maximum": 720 }
|
|
138
152
|
}
|
|
139
153
|
},
|
|
140
154
|
"fileHandlerDefinition": {
|
|
@@ -145,8 +159,9 @@
|
|
|
145
159
|
"extensions": {
|
|
146
160
|
"type": "array",
|
|
147
161
|
"minItems": 1,
|
|
148
|
-
"maxItems":
|
|
149
|
-
"
|
|
162
|
+
"maxItems": 16,
|
|
163
|
+
"uniqueItems": true,
|
|
164
|
+
"items": { "type": "string", "pattern": "^\\.[a-z0-9][a-z0-9.-]{0,39}$" }
|
|
150
165
|
},
|
|
151
166
|
"mode": { "type": "string", "enum": ["preview", "editor"] },
|
|
152
167
|
"view": { "$ref": "#/$defs/pluginKey" },
|
|
@@ -158,9 +173,9 @@
|
|
|
158
173
|
"additionalProperties": false,
|
|
159
174
|
"required": ["name", "label", "description", "effect", "parameters"],
|
|
160
175
|
"properties": {
|
|
161
|
-
"name": { "$ref": "#/$defs/
|
|
162
|
-
"label": { "type": "string", "minLength": 1, "maxLength":
|
|
163
|
-
"description": { "type": "string", "minLength": 1, "maxLength":
|
|
176
|
+
"name": { "$ref": "#/$defs/assistantToolName" },
|
|
177
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 60 },
|
|
178
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 300 },
|
|
164
179
|
"effect": { "type": "string", "enum": ["read", "write", "external"] },
|
|
165
180
|
"parameters": { "type": "object" }
|
|
166
181
|
}
|
|
@@ -170,9 +185,9 @@
|
|
|
170
185
|
"additionalProperties": false,
|
|
171
186
|
"required": ["id", "label", "description"],
|
|
172
187
|
"properties": {
|
|
173
|
-
"id": { "$ref": "#/$defs/
|
|
174
|
-
"label": { "type": "string", "minLength": 1, "maxLength":
|
|
175
|
-
"description": { "type": "string", "minLength": 1, "maxLength":
|
|
188
|
+
"id": { "$ref": "#/$defs/assistantContextId" },
|
|
189
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 60 },
|
|
190
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 200 },
|
|
176
191
|
"maxBytes": { "type": "integer", "minimum": 256, "maximum": 16000 }
|
|
177
192
|
}
|
|
178
193
|
},
|
|
@@ -181,8 +196,8 @@
|
|
|
181
196
|
"additionalProperties": false,
|
|
182
197
|
"required": ["name", "description"],
|
|
183
198
|
"properties": {
|
|
184
|
-
"name": { "$ref": "#/$defs/
|
|
185
|
-
"description": { "type": "string", "minLength": 1, "maxLength":
|
|
199
|
+
"name": { "$ref": "#/$defs/serviceName" },
|
|
200
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 240 },
|
|
186
201
|
"parameters": { "type": "object" },
|
|
187
202
|
"returns": { "type": "object" }
|
|
188
203
|
}
|
|
@@ -192,8 +207,8 @@
|
|
|
192
207
|
"additionalProperties": false,
|
|
193
208
|
"required": ["pluginId", "service"],
|
|
194
209
|
"properties": {
|
|
195
|
-
"pluginId": { "$ref": "#/$defs/
|
|
196
|
-
"service": { "$ref": "#/$defs/
|
|
210
|
+
"pluginId": { "$ref": "#/$defs/pluginKey" },
|
|
211
|
+
"service": { "$ref": "#/$defs/serviceName" },
|
|
197
212
|
"optional": { "type": "boolean" }
|
|
198
213
|
}
|
|
199
214
|
},
|
|
@@ -204,17 +219,19 @@
|
|
|
204
219
|
"properties": {
|
|
205
220
|
"key": { "$ref": "#/$defs/pluginKey" },
|
|
206
221
|
"label": { "type": "string", "minLength": 1, "maxLength": 80 },
|
|
207
|
-
"description": { "type": "string", "maxLength":
|
|
222
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 200 },
|
|
208
223
|
"type": { "type": "string", "enum": ["string", "number", "boolean", "select"] },
|
|
209
224
|
"default": { "type": ["string", "number", "boolean"] },
|
|
210
225
|
"options": {
|
|
211
226
|
"type": "array",
|
|
227
|
+
"minItems": 1,
|
|
228
|
+
"maxItems": 30,
|
|
212
229
|
"items": {
|
|
213
230
|
"type": "object",
|
|
214
231
|
"additionalProperties": false,
|
|
215
232
|
"required": ["value", "label"],
|
|
216
233
|
"properties": {
|
|
217
|
-
"value": { "type": "string", "minLength": 1, "maxLength":
|
|
234
|
+
"value": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
218
235
|
"label": { "type": "string", "minLength": 1, "maxLength": 80 }
|
|
219
236
|
}
|
|
220
237
|
}
|
|
@@ -226,7 +243,7 @@
|
|
|
226
243
|
},
|
|
227
244
|
"activationEvent": {
|
|
228
245
|
"type": "string",
|
|
229
|
-
"pattern": "^(onStartup|onCommand:[a-z][a-z0-9.-]{0,79}|onView:[a-z][a-z0-9.-]{0,79}|onFile:\\.[a-z0-9][a-z0-9.-]{0,
|
|
246
|
+
"pattern": "^(onStartup|onCommand:[a-z][a-z0-9.-]{0,79}|onView:[a-z][a-z0-9.-]{0,79}|onFile:\\.[a-z0-9][a-z0-9.-]{0,39}|onAssistantTool:[a-z][a-z0-9_-]{0,47}|onAssistantContext:[a-z][a-z0-9_-]{0,31}|onService:[a-z][a-z0-9_.-]{0,47})$"
|
|
230
247
|
},
|
|
231
248
|
"automationDefinition": {
|
|
232
249
|
"type": "object",
|
|
@@ -344,12 +361,14 @@
|
|
|
344
361
|
},
|
|
345
362
|
"networkOrigins": {
|
|
346
363
|
"type": "array",
|
|
347
|
-
"maxItems":
|
|
364
|
+
"maxItems": 50,
|
|
365
|
+
"uniqueItems": true,
|
|
348
366
|
"items": { "type": "string", "format": "uri", "pattern": "^https://" }
|
|
349
367
|
},
|
|
350
368
|
"nativeCommands": {
|
|
351
369
|
"type": "array",
|
|
352
|
-
"maxItems":
|
|
370
|
+
"maxItems": 50,
|
|
371
|
+
"uniqueItems": true,
|
|
353
372
|
"items": { "$ref": "#/$defs/pluginKey" }
|
|
354
373
|
},
|
|
355
374
|
"commands": {
|
|
@@ -359,58 +378,68 @@
|
|
|
359
378
|
},
|
|
360
379
|
"slashCommands": {
|
|
361
380
|
"type": "array",
|
|
381
|
+
"minItems": 1,
|
|
362
382
|
"maxItems": 20,
|
|
363
383
|
"items": { "$ref": "#/$defs/slashCommandDefinition" }
|
|
364
384
|
},
|
|
365
385
|
"views": {
|
|
366
386
|
"type": "array",
|
|
367
|
-
"maxItems":
|
|
387
|
+
"maxItems": 10,
|
|
368
388
|
"items": { "$ref": "#/$defs/viewDefinition" }
|
|
369
389
|
},
|
|
370
390
|
"codeBlockRenderers": {
|
|
371
391
|
"type": "array",
|
|
392
|
+
"minItems": 1,
|
|
372
393
|
"maxItems": 10,
|
|
373
394
|
"items": { "$ref": "#/$defs/codeBlockRendererDefinition" }
|
|
374
395
|
},
|
|
375
396
|
"fileHandlers": {
|
|
376
397
|
"type": "array",
|
|
398
|
+
"minItems": 1,
|
|
377
399
|
"maxItems": 10,
|
|
378
400
|
"items": { "$ref": "#/$defs/fileHandlerDefinition" }
|
|
379
401
|
},
|
|
380
402
|
"assistantTools": {
|
|
381
403
|
"type": "array",
|
|
382
|
-
"
|
|
404
|
+
"minItems": 1,
|
|
405
|
+
"maxItems": 16,
|
|
383
406
|
"items": { "$ref": "#/$defs/assistantToolDefinition" }
|
|
384
407
|
},
|
|
385
408
|
"assistantContextProviders": {
|
|
386
409
|
"type": "array",
|
|
387
|
-
"
|
|
410
|
+
"minItems": 1,
|
|
411
|
+
"maxItems": 8,
|
|
388
412
|
"items": { "$ref": "#/$defs/assistantContextProviderDefinition" }
|
|
389
413
|
},
|
|
390
414
|
"services": {
|
|
391
415
|
"type": "array",
|
|
416
|
+
"minItems": 1,
|
|
392
417
|
"maxItems": 16,
|
|
393
418
|
"items": { "$ref": "#/$defs/serviceDefinition" }
|
|
394
419
|
},
|
|
395
420
|
"usesServices": {
|
|
396
421
|
"type": "array",
|
|
397
|
-
"
|
|
422
|
+
"minItems": 1,
|
|
423
|
+
"maxItems": 20,
|
|
398
424
|
"items": { "$ref": "#/$defs/serviceDependency" }
|
|
399
425
|
},
|
|
400
426
|
"settingsSchema": {
|
|
401
427
|
"type": "array",
|
|
402
|
-
"
|
|
428
|
+
"minItems": 1,
|
|
429
|
+
"maxItems": 30,
|
|
403
430
|
"items": { "$ref": "#/$defs/settingField" }
|
|
404
431
|
},
|
|
405
432
|
"activationEvents": {
|
|
406
433
|
"type": "array",
|
|
407
|
-
"
|
|
434
|
+
"minItems": 1,
|
|
435
|
+
"maxItems": 20,
|
|
408
436
|
"uniqueItems": true,
|
|
409
437
|
"items": { "$ref": "#/$defs/activationEvent" }
|
|
410
438
|
},
|
|
411
439
|
"automations": {
|
|
412
440
|
"type": "array",
|
|
413
|
-
"
|
|
441
|
+
"minItems": 1,
|
|
442
|
+
"maxItems": 10,
|
|
414
443
|
"items": { "$ref": "#/$defs/automationDefinition" }
|
|
415
444
|
}
|
|
416
445
|
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tano.page/protocol",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts",
|
|
7
|
+
"./agent-identities": "./src/domain/agent-identities.ts",
|
|
7
8
|
"./agent-inbox": "./src/domain/agent-inbox.ts",
|
|
9
|
+
"./assistant-completion": "./src/domain/assistant-completion.ts",
|
|
10
|
+
"./assistant-engine": "./src/domain/assistant-engine.ts",
|
|
11
|
+
"./assistant-execution": "./src/domain/assistant-execution.ts",
|
|
8
12
|
"./assistant-files": "./src/domain/assistant-files.ts",
|
|
9
13
|
"./assistant-history": "./src/domain/assistant-history.ts",
|
|
14
|
+
"./assistant-language": "./src/domain/assistant-language.ts",
|
|
15
|
+
"./assistant-prompts": "./src/domain/assistant-prompts.ts",
|
|
16
|
+
"./assistant-structures": "./src/domain/assistant-structures.ts",
|
|
17
|
+
"./assistant-tasks": "./src/domain/assistant-tasks.ts",
|
|
10
18
|
"./features": "./src/features.ts",
|
|
11
19
|
"./frontmatter": "./src/frontmatter.ts",
|
|
12
20
|
"./markdown-file-metadata": "./src/markdown-file-metadata.ts",
|
|
21
|
+
"./plugin-data": "./src/domain/plugin-data.ts",
|
|
22
|
+
"./plugin-sdk": "./src/domain/plugin-sdk.ts",
|
|
13
23
|
"./untrusted-data": "./src/domain/untrusted-data.ts",
|
|
14
24
|
"./workspace-comments": "./src/domain/workspace-comments.ts",
|
|
15
25
|
"./manifest.schema.json": "./manifest.schema.json"
|
|
@@ -32,8 +42,8 @@
|
|
|
32
42
|
"url": "git+https://github.com/Dramwig/Tano.git"
|
|
33
43
|
},
|
|
34
44
|
"tanoCore": {
|
|
35
|
-
"sourceCommit": "
|
|
36
|
-
"version": "0.
|
|
45
|
+
"sourceCommit": "0eead85177b8d5010d0daea26f1995a27f9270a9",
|
|
46
|
+
"version": "0.16.0",
|
|
37
47
|
"importName": "@tano/protocol"
|
|
38
48
|
}
|
|
39
49
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { AssistantTransport } from '../assistant';
|
|
2
|
+
import { isAgentPerson, type AgentBinding } from '../mentions';
|
|
3
|
+
import { parseObject } from '../objects';
|
|
4
|
+
import type { WorkspaceAdapter } from '../types';
|
|
5
|
+
import { objectDirectory } from '../workspace-paths';
|
|
6
|
+
import { AgentTaskQueue } from './agent-inbox';
|
|
7
|
+
|
|
8
|
+
/** Reserve the binding before creating its portable identity, allowing crash-safe repair. */
|
|
9
|
+
export async function ensureAgentIdentities(
|
|
10
|
+
workspaceId: string,
|
|
11
|
+
adapter: Pick<WorkspaceAdapter, 'refresh' | 'read' | 'write' | 'create' | 'reserveObjectId'>,
|
|
12
|
+
transport: Pick<AssistantTransport, 'inbox' | 'connections' | 'models' | 'hostedInbox'>,
|
|
13
|
+
signal?: AbortSignal,
|
|
14
|
+
) {
|
|
15
|
+
const errors: string[] = [];
|
|
16
|
+
if (!transport.inbox || !adapter.reserveObjectId) return errors;
|
|
17
|
+
const queue = new AgentTaskQueue(transport.inbox);
|
|
18
|
+
const connections = await transport.connections();
|
|
19
|
+
for (const connection of connections) {
|
|
20
|
+
if (connection.platformProvider && transport.hostedInbox) continue;
|
|
21
|
+
try {
|
|
22
|
+
signal?.throwIfAborted();
|
|
23
|
+
const inbox = await transport.inbox.load(workspaceId);
|
|
24
|
+
let binding = inbox.bindings.find((item) => item.connectionId === connection.id);
|
|
25
|
+
if (!binding) {
|
|
26
|
+
let models: string[] = [];
|
|
27
|
+
try {
|
|
28
|
+
models = await transport.models(
|
|
29
|
+
connection.id,
|
|
30
|
+
signal ? AbortSignal.any([signal, AbortSignal.timeout(30_000)]) : AbortSignal.timeout(30_000),
|
|
31
|
+
);
|
|
32
|
+
} catch (reason) {
|
|
33
|
+
errors.push(`${connection.name} · ${String(reason)}`);
|
|
34
|
+
}
|
|
35
|
+
signal?.throwIfAborted();
|
|
36
|
+
const workspace = await adapter.refresh();
|
|
37
|
+
if (workspace.id !== workspaceId || workspace.readOnly) return errors;
|
|
38
|
+
const personId = await adapter.reserveObjectId(inbox.bindings.map((item) => item.personId));
|
|
39
|
+
const candidate: AgentBinding = {
|
|
40
|
+
personId,
|
|
41
|
+
connectionId: connection.id,
|
|
42
|
+
model: models[0] ?? '',
|
|
43
|
+
enabled: true,
|
|
44
|
+
};
|
|
45
|
+
binding = await queue.update(workspaceId, (current) => {
|
|
46
|
+
const existing = current.bindings.find((item) => item.connectionId === connection.id);
|
|
47
|
+
if (existing) return { ...existing };
|
|
48
|
+
current.bindings.push(candidate);
|
|
49
|
+
return candidate;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const workspace = await adapter.refresh();
|
|
53
|
+
signal?.throwIfAborted();
|
|
54
|
+
if (workspace.id !== workspaceId || workspace.readOnly) return errors;
|
|
55
|
+
const path = `${objectDirectory}/${binding.personId}.tano.json`;
|
|
56
|
+
if (workspace.files.some((file) => file.path === path)) {
|
|
57
|
+
const object = parseObject((await adapter.read(path)).content);
|
|
58
|
+
if (object.id !== binding.personId || !isAgentPerson(object))
|
|
59
|
+
throw new Error('机器人身份与已有对象冲突');
|
|
60
|
+
if (!binding.provisioned) {
|
|
61
|
+
await queue.update(workspaceId, current => {
|
|
62
|
+
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
63
|
+
if (saved) saved.provisioned = true;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (binding.provisioned) {
|
|
69
|
+
await queue.update(workspaceId, current => {
|
|
70
|
+
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
71
|
+
if (saved) saved.enabled = false;
|
|
72
|
+
});
|
|
73
|
+
errors.push(`${connection.name} · 机器人身份文件已移除,请从文件历史恢复后重新启用`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const object = {
|
|
77
|
+
version: 1,
|
|
78
|
+
id: binding.personId,
|
|
79
|
+
type: 'person',
|
|
80
|
+
name: connection.name,
|
|
81
|
+
description: '',
|
|
82
|
+
archived: false,
|
|
83
|
+
properties: { memberKind: 'agent' },
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
await adapter.create(path, JSON.stringify(object, null, 2) + '\n');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const current = await adapter.refresh();
|
|
89
|
+
if (!current.files.some((file) => file.path === path)) throw error;
|
|
90
|
+
const saved = parseObject((await adapter.read(path)).content);
|
|
91
|
+
if (saved.id !== binding.personId || !isAgentPerson(saved)) throw error;
|
|
92
|
+
}
|
|
93
|
+
await queue.update(workspaceId, current => {
|
|
94
|
+
const saved = current.bindings.find(item => item.personId === binding!.personId);
|
|
95
|
+
if (saved) saved.provisioned = true;
|
|
96
|
+
});
|
|
97
|
+
} catch (reason) {
|
|
98
|
+
errors.push(`${connection.name} · ${String(reason)}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
await queue.update(workspaceId, (inbox) => {
|
|
102
|
+
for (const binding of inbox.bindings) {
|
|
103
|
+
if (!connections.some((connection) => connection.id === binding.connectionId) ||
|
|
104
|
+
(transport.hostedInbox && connections.some(connection => connection.id === binding.connectionId && connection.platformProvider))) {
|
|
105
|
+
binding.enabled = false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return errors;
|
|
110
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { AssistantActivity, AssistantTerminationReason } from '../assistant';
|
|
2
|
+
|
|
3
|
+
export type AssistantTextPhase = Extract<AssistantActivity, { kind: 'text' }>['phase'];
|
|
4
|
+
export type AssistantTextSegment = { start: number; end: number; phase: AssistantTextPhase };
|
|
5
|
+
|
|
6
|
+
export class AssistantRunError extends Error {
|
|
7
|
+
constructor(public reason: AssistantTerminationReason, message: string) { super(message); }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function responsePhase(value: unknown): AssistantTextPhase | undefined {
|
|
11
|
+
if (value === 'commentary' || value === 'partial_answer') return 'working';
|
|
12
|
+
if (value === 'final_answer') return 'answer';
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const markers = [
|
|
17
|
+
{ prefix: '[[tano:progress]]', phase: 'working' },
|
|
18
|
+
{ prefix: '[[tano:final]]', phase: 'answer' },
|
|
19
|
+
] as const;
|
|
20
|
+
|
|
21
|
+
/** Only an exact leading protocol marker is interpreted; ordinary prose is never guessed. */
|
|
22
|
+
export class AssistantTextDecoder {
|
|
23
|
+
private buffer = '';
|
|
24
|
+
private started = false;
|
|
25
|
+
private afterMarker = false;
|
|
26
|
+
phase: AssistantTextPhase = 'pending';
|
|
27
|
+
constructor(private emit: (text: string, phase: AssistantTextPhase) => void) {}
|
|
28
|
+
push(text: string, nativePhase?: AssistantTextPhase) {
|
|
29
|
+
if (nativePhase) this.phase = nativePhase;
|
|
30
|
+
if (this.started && !this.afterMarker) { this.emit(text, this.phase); return; }
|
|
31
|
+
this.buffer += text;
|
|
32
|
+
if (!this.started) {
|
|
33
|
+
const marker = markers.find(marker => this.buffer.startsWith(marker.prefix));
|
|
34
|
+
if (marker) {
|
|
35
|
+
this.phase = nativePhase ?? marker.phase;
|
|
36
|
+
this.buffer = this.buffer.slice(marker.prefix.length);
|
|
37
|
+
this.afterMarker = true;
|
|
38
|
+
} else if (markers.some(marker => marker.prefix.startsWith(this.buffer))) return;
|
|
39
|
+
this.started = true;
|
|
40
|
+
}
|
|
41
|
+
if (this.afterMarker) {
|
|
42
|
+
if (!this.buffer || this.buffer === '\r') return;
|
|
43
|
+
this.buffer = this.buffer.replace(/^\r?\n/, '');
|
|
44
|
+
this.afterMarker = false;
|
|
45
|
+
}
|
|
46
|
+
if (this.buffer) this.emit(this.buffer, this.phase);
|
|
47
|
+
this.buffer = '';
|
|
48
|
+
}
|
|
49
|
+
finish() {
|
|
50
|
+
if (!this.started && this.buffer) throw new AssistantRunError('incomplete', '模型的回答阶段标记未完整返回');
|
|
51
|
+
if (this.buffer) this.emit(this.buffer, this.phase);
|
|
52
|
+
this.buffer = '';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function decodeAssistantText(text: string, phase?: AssistantTextPhase) {
|
|
57
|
+
let content = '';
|
|
58
|
+
const decoder = new AssistantTextDecoder(value => { content += value; });
|
|
59
|
+
decoder.push(text, phase); decoder.finish();
|
|
60
|
+
return { content, phase: phase ?? decoder.phase };
|
|
61
|
+
}
|