@sixb/agent 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/artifacts/publish-tool.d.ts +26 -44
- package/dist/artifacts/publish-tool.d.ts.map +1 -1
- package/dist/ask-tool.d.ts +62 -0
- package/dist/ask-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +1 -1
- package/dist/attachments.d.ts.map +1 -1
- package/dist/background-commands.d.ts +3 -3
- package/dist/background-commands.d.ts.map +1 -1
- package/dist/background-notices.d.ts.map +1 -1
- package/dist/child-files.d.ts +1 -1
- package/dist/child-questions.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1380 -596
- package/dist/index.js.map +30 -26
- package/dist/run-agent-turn.d.ts.map +1 -1
- package/dist/run-conversation-turn.d.ts.map +1 -1
- package/dist/run-environment.d.ts +4 -1
- package/dist/run-environment.d.ts.map +1 -1
- package/dist/run-subagent.d.ts +1 -1
- package/dist/subagent-tools.d.ts +20 -1
- package/dist/subagent-tools.d.ts.map +1 -1
- package/dist/tools/background.d.ts +50 -18
- package/dist/tools/background.d.ts.map +1 -1
- package/dist/tools/bash.d.ts +1 -0
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/edit-text.d.ts +22 -0
- package/dist/tools/edit-text.d.ts.map +1 -0
- package/dist/tools/edit.d.ts +54 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/file-writes.d.ts +52 -0
- package/dist/tools/file-writes.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts.map +1 -1
- package/dist/tools/read.d.ts +16 -3
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/view-file.d.ts +20 -23
- package/dist/tools/view-file.d.ts.map +1 -1
- package/dist/tools/web-fetch/tool.d.ts +1 -0
- package/dist/tools/web-fetch/tool.d.ts.map +1 -1
- package/dist/tools/web-search.d.ts +1 -0
- package/dist/tools/web-search.d.ts.map +1 -1
- package/dist/tools/write.d.ts +36 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/agent-prompt.ts +9 -9
- package/src/artifacts/publish-tool.ts +76 -94
- package/src/ask-tool.ts +223 -0
- package/src/attachments.ts +7 -6
- package/src/background-commands.ts +5 -5
- package/src/background-notices.ts +5 -12
- package/src/child-files.ts +1 -1
- package/src/child-questions.ts +1 -1
- package/src/context-compaction.ts +4 -1
- package/src/index.ts +1 -1
- package/src/run-agent-turn.ts +14 -4
- package/src/run-child.ts +1 -1
- package/src/run-conversation-turn.ts +4 -1
- package/src/run-environment.ts +36 -21
- package/src/run-subagent.ts +3 -3
- package/src/skills/sixb-artifacts/SKILL.md +7 -7
- package/src/skills/sixb-artifacts/canvas.md +2 -2
- package/src/skills/sixb-artifacts/page.md +2 -2
- package/src/skills/sixb-artifacts/slides.md +2 -2
- package/src/subagent-tools.ts +115 -81
- package/src/tools/background.ts +227 -67
- package/src/tools/bash.ts +2 -1
- package/src/tools/edit-text.ts +241 -0
- package/src/tools/edit.ts +166 -0
- package/src/tools/file-writes.ts +270 -0
- package/src/tools/model-spec.ts +9 -13
- package/src/tools/read.ts +75 -9
- package/src/tools/tool-outputs.ts +2 -2
- package/src/tools/view-file.ts +78 -80
- package/src/tools/web-fetch/tool.ts +2 -1
- package/src/tools/web-search.ts +1 -0
- package/src/tools/write.ts +95 -0
- package/dist/ask-parent-tool.d.ts +0 -36
- package/dist/ask-parent-tool.d.ts.map +0 -1
- package/src/ask-parent-tool.ts +0 -100
package/src/tools/view-file.ts
CHANGED
|
@@ -2,9 +2,7 @@ import { posix } from "node:path"
|
|
|
2
2
|
import type { AgentToolFiles, FileRef, JsonValue } from "@sixb/core"
|
|
3
3
|
import { AgentToolPublicError } from "@sixb/core"
|
|
4
4
|
import { inferFileMediaType } from "@sixb/core/internal/blob-storage"
|
|
5
|
-
import type { JsonObject, ModelTool } from "@sixb/core/models"
|
|
6
5
|
import type { PreparedAgentAttachmentContext } from "../attachments"
|
|
7
|
-
import { agentToolErrorText } from "../model-adapters"
|
|
8
6
|
import type { AgentSandboxFileRegistry } from "../sandbox-file-registry"
|
|
9
7
|
import { readSandboxFile, resolveSandboxPath, SandboxFileError } from "../sandbox-files"
|
|
10
8
|
import type { AgentSandboxHandle } from "../sandbox-handle"
|
|
@@ -13,24 +11,14 @@ import { AGENT_FILE_MAX_BYTES } from "./tool-files"
|
|
|
13
11
|
|
|
14
12
|
const VIEW_FILE_TIMEOUT_MS = 30_000
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
type: "object",
|
|
22
|
-
properties: { path: { type: "string", minLength: 1 } },
|
|
23
|
-
required: ["path"],
|
|
24
|
-
additionalProperties: false,
|
|
25
|
-
} satisfies JsonObject,
|
|
26
|
-
} as const
|
|
14
|
+
/**
|
|
15
|
+
* What `read` took over: earlier threads hold calls to `view_file`, and their results replay as
|
|
16
|
+
* they were stored, images included.
|
|
17
|
+
*/
|
|
18
|
+
export const LEGACY_VIEW_FILE_TOOL = "view_file"
|
|
27
19
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function createViewFileTool(input: {
|
|
33
|
-
readonly resolveSandbox: () => Promise<AgentSandboxHandle>
|
|
20
|
+
/** What `read` needs to show a file as itself, an image or a PDF, rather than as text. */
|
|
21
|
+
export interface SandboxFileViewer {
|
|
34
22
|
readonly attachments: PreparedAgentAttachmentContext
|
|
35
23
|
readonly registry: AgentSandboxFileRegistry
|
|
36
24
|
readonly filesForToolCall: (options: {
|
|
@@ -42,67 +30,79 @@ export function createViewFileTool(input: {
|
|
|
42
30
|
readonly signal: AbortSignal
|
|
43
31
|
readonly toolCallId: string
|
|
44
32
|
}) => AgentToolModelOutput | Promise<AgentToolModelOutput>
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
...VIEW_FILE_TOOL_SPEC,
|
|
48
|
-
parseInput: parseViewFileInput,
|
|
49
|
-
async execute({ path }, { signal, toolCallId }): Promise<JsonValue> {
|
|
50
|
-
signal.throwIfAborted()
|
|
51
|
-
const handle = await input.resolveSandbox()
|
|
52
|
-
let absolutePath: string
|
|
53
|
-
try {
|
|
54
|
-
absolutePath = resolveSandboxPath(handle, path)
|
|
55
|
-
} catch (error) {
|
|
56
|
-
throw toViewFileError(error)
|
|
57
|
-
}
|
|
58
|
-
const knownFileRef =
|
|
59
|
-
input.registry.get(absolutePath) ??
|
|
60
|
-
preparedFileRefAtPath(input.attachments, handle.sandbox.workingDirectory, absolutePath)
|
|
33
|
+
}
|
|
61
34
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
35
|
+
/**
|
|
36
|
+
* A sandbox file as a rich result: the file itself, stored with the call, which the run's media
|
|
37
|
+
* bridge shows the model as an image or a document when it can, and as metadata otherwise. An
|
|
38
|
+
* attachment, or a file a tool stored, is the snapshot it already is.
|
|
39
|
+
*/
|
|
40
|
+
export async function viewSandboxFile(
|
|
41
|
+
viewer: SandboxFileViewer,
|
|
42
|
+
handle: AgentSandboxHandle,
|
|
43
|
+
input: { readonly path: string; readonly toolCallId: string; readonly signal: AbortSignal }
|
|
44
|
+
): Promise<JsonValue> {
|
|
45
|
+
input.signal.throwIfAborted()
|
|
46
|
+
let absolutePath: string
|
|
47
|
+
try {
|
|
48
|
+
absolutePath = resolveSandboxPath(handle, input.path)
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw toViewError(error)
|
|
51
|
+
}
|
|
52
|
+
const knownFileRef =
|
|
53
|
+
viewer.registry.get(absolutePath) ??
|
|
54
|
+
preparedFileRefAtPath(viewer.attachments, handle.sandbox.workingDirectory, absolutePath)
|
|
82
55
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
56
|
+
let fileRef: FileRef
|
|
57
|
+
if (knownFileRef) {
|
|
58
|
+
fileRef = knownFileRef
|
|
59
|
+
} else {
|
|
60
|
+
const bytes = await readSandboxFile(handle, {
|
|
61
|
+
path: absolutePath,
|
|
62
|
+
maxBytes: AGENT_FILE_MAX_BYTES,
|
|
63
|
+
signal: input.signal,
|
|
64
|
+
timeoutMs: VIEW_FILE_TIMEOUT_MS,
|
|
65
|
+
}).catch((error: unknown) => {
|
|
66
|
+
throw toViewError(error)
|
|
67
|
+
})
|
|
68
|
+
const fileName = safePublishedFileName(posix.basename(absolutePath))
|
|
69
|
+
const file = await viewer
|
|
70
|
+
.filesForToolCall({ toolCallId: input.toolCallId, signal: input.signal })
|
|
71
|
+
.put({ body: bytes, fileName, mediaType: inferFileMediaType(fileName, bytes) })
|
|
72
|
+
fileRef = file.fileRef
|
|
93
73
|
}
|
|
74
|
+
|
|
75
|
+
// Prepared attachments and files tools stored are immutable snapshots and can be reused.
|
|
76
|
+
// Arbitrary bash paths stay mutable, so do not cache their published snapshot at the source
|
|
77
|
+
// path; a later read must read the path again.
|
|
78
|
+
if (knownFileRef) viewer.registry.register(absolutePath, fileRef)
|
|
79
|
+
return richViewFileResult(fileRef)
|
|
94
80
|
}
|
|
95
81
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
82
|
+
/** Whether `path` names an attachment or a file a tool stored, which `read` shows as itself. */
|
|
83
|
+
export function isKnownSandboxFile(
|
|
84
|
+
viewer: SandboxFileViewer,
|
|
85
|
+
handle: AgentSandboxHandle,
|
|
86
|
+
path: string
|
|
87
|
+
): boolean {
|
|
88
|
+
let absolutePath: string
|
|
89
|
+
try {
|
|
90
|
+
absolutePath = resolveSandboxPath(handle, path)
|
|
91
|
+
} catch {
|
|
92
|
+
return false
|
|
99
93
|
}
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
94
|
+
const fileRef =
|
|
95
|
+
viewer.registry.get(absolutePath) ??
|
|
96
|
+
preparedFileRefAtPath(viewer.attachments, handle.sandbox.workingDirectory, absolutePath)
|
|
97
|
+
return fileRef !== undefined && !isTextMediaType(fileRef.mediaType)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isTextMediaType(mediaType: string | undefined): boolean {
|
|
101
|
+
return (
|
|
102
|
+
mediaType !== undefined &&
|
|
103
|
+
(mediaType.startsWith("text/") ||
|
|
104
|
+
/^application\/(json|xml|yaml|x-yaml|javascript|typescript|csv|x-sh)\b/.test(mediaType))
|
|
105
|
+
)
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
function preparedFileRefAtPath(
|
|
@@ -146,10 +146,8 @@ function safePublishedFileName(value: string): string {
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
/** A sandbox file refusal becomes the message the Agent sees; anything else passes through. */
|
|
149
|
-
function
|
|
150
|
-
return error instanceof SandboxFileError
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function viewFileError(message: string): AgentToolPublicError {
|
|
154
|
-
return new AgentToolPublicError(`[SixbAgent] view_file ${message}.`)
|
|
149
|
+
function toViewError(error: unknown): unknown {
|
|
150
|
+
return error instanceof SandboxFileError
|
|
151
|
+
? new AgentToolPublicError(`[SixbAgent] read ${error.message}.`)
|
|
152
|
+
: error
|
|
155
153
|
}
|
|
@@ -54,6 +54,7 @@ const ANSWER_INSTRUCTIONS = [
|
|
|
54
54
|
|
|
55
55
|
export const WEB_FETCH_TOOL_SPEC = {
|
|
56
56
|
name: "web_fetch",
|
|
57
|
+
strict: true,
|
|
57
58
|
description:
|
|
58
59
|
"Read a web page, PDF or image by URL. A large page is saved to a file. Pass a question to get an answer from the page.",
|
|
59
60
|
inputSchema: {
|
|
@@ -266,7 +267,7 @@ function savedNote(output: WebFetchToolOutput): string {
|
|
|
266
267
|
if (/\.(?:md|txt)$/.test(output.path ?? "")) {
|
|
267
268
|
return `The page (${size} sent) is saved as text at ${output.path}: read it in pieces with the read tool, or search it with grep.`
|
|
268
269
|
}
|
|
269
|
-
return `The file (${output.contentType || "unknown type"}, ${size}) is saved at ${output.path}: open it with
|
|
270
|
+
return `The file (${output.contentType || "unknown type"}, ${size}) is saved at ${output.path}: open it with read.`
|
|
270
271
|
}
|
|
271
272
|
|
|
272
273
|
function truncatedAnswerNote(output: WebFetchToolOutput): string {
|
package/src/tools/web-search.ts
CHANGED
|
@@ -15,6 +15,7 @@ const MAX_QUERY_CHARACTERS = 2_000
|
|
|
15
15
|
|
|
16
16
|
export const WEB_SEARCH_TOOL_SPEC = {
|
|
17
17
|
name: "web_search",
|
|
18
|
+
strict: true,
|
|
18
19
|
description:
|
|
19
20
|
"Search the web for current or external information. Returns up to 8 results, each a title, URL, date and snippet.",
|
|
20
21
|
inputSchema: {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue, ModelTool } from "@sixb/core/models"
|
|
2
|
+
import { agentToolErrorText } from "../model-adapters"
|
|
3
|
+
import type { AgentSandboxHandle } from "../sandbox-handle"
|
|
4
|
+
import { assertReadAsIs, objectInput, pathInput } from "./edit"
|
|
5
|
+
import {
|
|
6
|
+
commitSandboxFile,
|
|
7
|
+
type FileReadLedger,
|
|
8
|
+
loadSandboxFile,
|
|
9
|
+
MAX_EDITED_FILE_BYTES,
|
|
10
|
+
publicError,
|
|
11
|
+
} from "./file-writes"
|
|
12
|
+
|
|
13
|
+
export const WRITE_TOOL_SPEC = {
|
|
14
|
+
name: "write",
|
|
15
|
+
strict: true,
|
|
16
|
+
description:
|
|
17
|
+
"Create a file, or replace one whole; missing parent directories are created. Replacing an existing file needs a read of it first. To change part of a file, use edit.",
|
|
18
|
+
inputSchema: {
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
path: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description:
|
|
24
|
+
"Path relative to the working directory, or an absolute path in the run's context directory.",
|
|
25
|
+
},
|
|
26
|
+
content: { type: "string", description: "The file's whole new contents." },
|
|
27
|
+
},
|
|
28
|
+
required: ["path", "content"],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
} satisfies JsonObject,
|
|
31
|
+
} as const
|
|
32
|
+
|
|
33
|
+
export interface WriteToolInput {
|
|
34
|
+
readonly path: string
|
|
35
|
+
readonly content: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface WriteToolOutput {
|
|
39
|
+
readonly path: string
|
|
40
|
+
/** True when the file did not exist before. */
|
|
41
|
+
readonly created: boolean
|
|
42
|
+
readonly lines: number
|
|
43
|
+
readonly bytes: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createWriteTool(
|
|
47
|
+
resolveSandbox: () => Promise<AgentSandboxHandle>,
|
|
48
|
+
ledger: FileReadLedger
|
|
49
|
+
): ModelTool<WriteToolInput> {
|
|
50
|
+
return {
|
|
51
|
+
...WRITE_TOOL_SPEC,
|
|
52
|
+
parseInput: parseWriteInput,
|
|
53
|
+
async execute(input, { signal }): Promise<JsonValue> {
|
|
54
|
+
const bytes = new TextEncoder().encode(input.content)
|
|
55
|
+
if (bytes.byteLength > MAX_EDITED_FILE_BYTES) {
|
|
56
|
+
throw publicError(
|
|
57
|
+
"write",
|
|
58
|
+
`content is larger than ${MAX_EDITED_FILE_BYTES / 1024 / 1024} MiB: write it with bash.`
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
const handle = await resolveSandbox()
|
|
62
|
+
const loaded = await loadSandboxFile(handle, { path: input.path, tool: "write", signal })
|
|
63
|
+
if (loaded.kind === "file") assertReadAsIs(ledger, loaded, input.path, "write")
|
|
64
|
+
|
|
65
|
+
const written = await commitSandboxFile(handle, {
|
|
66
|
+
path: loaded.kind === "file" ? loaded.realPath : input.path,
|
|
67
|
+
expected: loaded.kind === "file" ? loaded.fingerprint : "missing",
|
|
68
|
+
bytes,
|
|
69
|
+
tool: "write",
|
|
70
|
+
signal,
|
|
71
|
+
})
|
|
72
|
+
ledger.record(written.realPath, written.fingerprint)
|
|
73
|
+
return {
|
|
74
|
+
path: input.path,
|
|
75
|
+
created: loaded.kind === "missing",
|
|
76
|
+
lines: lineCount(input.content),
|
|
77
|
+
bytes: bytes.byteLength,
|
|
78
|
+
} satisfies WriteToolOutput
|
|
79
|
+
},
|
|
80
|
+
errorText: agentToolErrorText,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function lineCount(content: string): number {
|
|
85
|
+
if (content === "") return 0
|
|
86
|
+
const newlines = content.split("\n").length - 1
|
|
87
|
+
return content.endsWith("\n") ? newlines : newlines + 1
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function parseWriteInput(value: unknown): WriteToolInput {
|
|
91
|
+
const input = objectInput(value, "write", ["path", "content"])
|
|
92
|
+
const path = pathInput(input.path, "write")
|
|
93
|
+
if (typeof input.content !== "string") throw publicError("write", "content must be a string.")
|
|
94
|
+
return { path, content: input.content }
|
|
95
|
+
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { type AgentMessage } from "@sixb/core";
|
|
2
|
-
import { type ModelTool } from "@sixb/core/models";
|
|
3
|
-
import type { SubagentInbox } from "./subagent-inbox";
|
|
4
|
-
export interface AskParentInput {
|
|
5
|
-
readonly question: string;
|
|
6
|
-
}
|
|
7
|
-
/** What `ask_parent` answers: the parent's answer, or that it comes in a later turn. */
|
|
8
|
-
export type AskParentOutput = {
|
|
9
|
-
readonly answer: string;
|
|
10
|
-
}
|
|
11
|
-
/** `question`: its id, which the answer of a later turn replies to. */
|
|
12
|
-
| {
|
|
13
|
-
readonly status: "deferred";
|
|
14
|
-
readonly question: string;
|
|
15
|
-
readonly note: string;
|
|
16
|
-
}
|
|
17
|
-
/** It paused on a held call too: only a decision continues it, without an answer. */
|
|
18
|
-
| {
|
|
19
|
-
readonly status: "unanswered";
|
|
20
|
-
readonly question: string;
|
|
21
|
-
readonly note: string;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* What a question deferred in the step that held a call reads as once the child continues: the
|
|
25
|
-
* decision continues it, and no answer comes with it. Only a continuation reads it, as the child
|
|
26
|
-
* paused right after that step, so what its thread keeps may say so without changing a byte the
|
|
27
|
-
* model already read.
|
|
28
|
-
*/
|
|
29
|
-
export declare function unansweredQuestion<TMessage extends Pick<AgentMessage, "parts">>(messages: readonly TMessage[]): readonly TMessage[];
|
|
30
|
-
/**
|
|
31
|
-
* A child's question to its parent, which it waits for within its step. Its parent answers with
|
|
32
|
-
* `message_agent`; when its turn ends first, the child pauses on the question and a later turn
|
|
33
|
-
* continues it with the answer.
|
|
34
|
-
*/
|
|
35
|
-
export declare function createAskParentTool(inbox: SubagentInbox): ModelTool<AskParentInput>;
|
|
36
|
-
//# sourceMappingURL=ask-parent-tool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ask-parent-tool.d.ts","sourceRoot":"","sources":["../src/ask-parent-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAwB,MAAM,YAAY,CAAA;AACpE,OAAO,EAAiC,KAAK,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEjF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAErD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAC1B;AAED,wFAAwF;AACxF,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE;AAC7B,uEAAuE;GACrE;IAAE,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AACnF,qFAAqF;GACnF;IAAE,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAA;AASvF;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,SAAS,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,EAC7E,QAAQ,EAAE,SAAS,QAAQ,EAAE,GAC5B,SAAS,QAAQ,EAAE,CAiBrB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,aAAa,GAAG,SAAS,CAAC,cAAc,CAAC,CA+BnF"}
|
package/src/ask-parent-tool.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { type AgentMessage, AgentToolPublicError } from "@sixb/core"
|
|
2
|
-
import { assertJsonValue, isJsonObject, type ModelTool } from "@sixb/core/models"
|
|
3
|
-
import { agentToolErrorText } from "./model-adapters"
|
|
4
|
-
import type { SubagentInbox } from "./subagent-inbox"
|
|
5
|
-
|
|
6
|
-
export interface AskParentInput {
|
|
7
|
-
readonly question: string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** What `ask_parent` answers: the parent's answer, or that it comes in a later turn. */
|
|
11
|
-
export type AskParentOutput =
|
|
12
|
-
| { readonly answer: string }
|
|
13
|
-
/** `question`: its id, which the answer of a later turn replies to. */
|
|
14
|
-
| { readonly status: "deferred"; readonly question: string; readonly note: string }
|
|
15
|
-
/** It paused on a held call too: only a decision continues it, without an answer. */
|
|
16
|
-
| { readonly status: "unanswered"; readonly question: string; readonly note: string }
|
|
17
|
-
|
|
18
|
-
/** Read by the child only once it continues: its loop pauses right after the step that asked. */
|
|
19
|
-
const DEFERRED_NOTE =
|
|
20
|
-
"Your parent's turn ended before it could answer. You continue with its answer, a <sixb_parent_answer> block, once it has one."
|
|
21
|
-
|
|
22
|
-
const UNANSWERED_NOTE =
|
|
23
|
-
"Your parent's turn ended before it could answer, and you paused on a call that waits for the user's approval: you continue once the user decides, without an answer. Ask again if you still need one."
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* What a question deferred in the step that held a call reads as once the child continues: the
|
|
27
|
-
* decision continues it, and no answer comes with it. Only a continuation reads it, as the child
|
|
28
|
-
* paused right after that step, so what its thread keeps may say so without changing a byte the
|
|
29
|
-
* model already read.
|
|
30
|
-
*/
|
|
31
|
-
export function unansweredQuestion<TMessage extends Pick<AgentMessage, "parts">>(
|
|
32
|
-
messages: readonly TMessage[]
|
|
33
|
-
): readonly TMessage[] {
|
|
34
|
-
return messages.map((message) => ({
|
|
35
|
-
...message,
|
|
36
|
-
parts: message.parts.map((part) =>
|
|
37
|
-
part.type === "tool-call" &&
|
|
38
|
-
part.toolName === "ask_parent" &&
|
|
39
|
-
part.state === "output-available" &&
|
|
40
|
-
isJsonObject(part.output) &&
|
|
41
|
-
part.output.status === "deferred" &&
|
|
42
|
-
typeof part.output.question === "string"
|
|
43
|
-
? {
|
|
44
|
-
...part,
|
|
45
|
-
output: { status: "unanswered", question: part.output.question, note: UNANSWERED_NOTE },
|
|
46
|
-
}
|
|
47
|
-
: part
|
|
48
|
-
),
|
|
49
|
-
}))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* A child's question to its parent, which it waits for within its step. Its parent answers with
|
|
54
|
-
* `message_agent`; when its turn ends first, the child pauses on the question and a later turn
|
|
55
|
-
* continues it with the answer.
|
|
56
|
-
*/
|
|
57
|
-
export function createAskParentTool(inbox: SubagentInbox): ModelTool<AskParentInput> {
|
|
58
|
-
return {
|
|
59
|
-
name: "ask_parent",
|
|
60
|
-
description: [
|
|
61
|
-
"Ask your parent agent a question and wait for its answer, which this tool returns.",
|
|
62
|
-
"Ask only when you cannot go on without a decision that only your parent or the user can make, never what the project's data or your task answers. One question at a time.",
|
|
63
|
-
].join("\n"),
|
|
64
|
-
inputSchema: {
|
|
65
|
-
type: "object",
|
|
66
|
-
properties: {
|
|
67
|
-
question: {
|
|
68
|
-
type: "string",
|
|
69
|
-
minLength: 1,
|
|
70
|
-
description: "One short question, with the choices you see when there are some.",
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
required: ["question"],
|
|
74
|
-
additionalProperties: false,
|
|
75
|
-
},
|
|
76
|
-
parseInput: parseAskParentInput,
|
|
77
|
-
execute: async ({ question }, { signal, callId, toolCallId }) => {
|
|
78
|
-
const reply = await inbox.ask(question, `${callId}:${toolCallId}`, signal)
|
|
79
|
-
const output: AskParentOutput =
|
|
80
|
-
reply.kind === "answer"
|
|
81
|
-
? { answer: reply.text }
|
|
82
|
-
: { status: "deferred", question: reply.questionId, note: DEFERRED_NOTE }
|
|
83
|
-
assertJsonValue(output)
|
|
84
|
-
return output
|
|
85
|
-
},
|
|
86
|
-
errorText: agentToolErrorText,
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function parseAskParentInput(value: unknown): AskParentInput {
|
|
91
|
-
if (
|
|
92
|
-
!isJsonObject(value) ||
|
|
93
|
-
Object.keys(value).some((key) => key !== "question") ||
|
|
94
|
-
typeof value.question !== "string" ||
|
|
95
|
-
!value.question.trim()
|
|
96
|
-
) {
|
|
97
|
-
throw new AgentToolPublicError("[SixbAgent] ask_parent expects a non-empty question.")
|
|
98
|
-
}
|
|
99
|
-
return { question: value.question.trim() }
|
|
100
|
-
}
|