@gaoding/cli 1.1.0 → 1.3.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/contracts/operations/task.get/input.schema.json +14 -0
- package/contracts/operations/task.get/output.schema.json +103 -0
- package/contracts/operations/task.list/input.schema.json +18 -0
- package/contracts/operations/task.list/output.schema.json +53 -0
- package/dist/bin/agent-worker.js +41 -0
- package/dist/bin/gd-cli.js +2 -1
- package/dist/bin/insmind-cli.js +60 -0
- package/dist/bin/postinstall.js +1 -1
- package/dist/src/bootstrap/create-cli.js +16 -4
- package/dist/src/bootstrap/create-runtime.js +190 -33
- package/dist/src/bootstrap/run-agent-worker.js +37 -0
- package/dist/src/bootstrap/validators.js +17 -5
- package/dist/src/cli/agent-commands.js +40 -14
- package/dist/src/cli/auth-commands.js +13 -10
- package/dist/src/cli/credits-command.js +23 -0
- package/dist/src/cli/dam-commands.js +70 -69
- package/dist/src/cli/editor-commands.js +12 -11
- package/dist/src/cli/errors.js +60 -9
- package/dist/src/cli/model-commands.js +6 -5
- package/dist/src/cli/presenter.js +24 -6
- package/dist/src/cli/task-commands.js +48 -0
- package/dist/src/cli/tool-commands.js +38 -14
- package/dist/src/cli/update-command.js +4 -3
- package/dist/src/features/agent/creative-agent-adapter.js +9 -3
- package/dist/src/features/agent/creative-protocol.js +5 -16
- package/dist/src/features/agent/creative-stream.js +12 -1
- package/dist/src/features/agent/filems-resource-auth-key-provider.js +23 -0
- package/dist/src/features/agent/resource-authorizer.js +47 -0
- package/dist/src/features/agent/result-reader.js +30 -0
- package/dist/src/features/agent/use-cases.js +98 -95
- package/dist/src/features/auth/credential-store.js +1 -1
- package/dist/src/features/auth/sso-service.js +3 -1
- package/dist/src/features/auth/use-cases.js +38 -12
- package/dist/src/features/credits/use-cases.js +52 -0
- package/dist/src/features/dam/storage-upload.js +1 -1
- package/dist/src/features/editor/bridge-process.js +11 -4
- package/dist/src/features/editor/session-state.js +1 -1
- package/dist/src/features/editor/session.js +14 -8
- package/dist/src/features/org/use-cases.js +7 -0
- package/dist/src/features/skill/bundled-skills.js +13 -5
- package/dist/src/features/task/agent-background.js +63 -0
- package/dist/src/features/task/store.js +97 -0
- package/dist/src/features/task/types.js +1 -0
- package/dist/src/features/task/use-cases.js +139 -0
- package/dist/src/features/tool/catalog.js +13 -13
- package/dist/src/features/tool/tool-api-adapter.js +53 -36
- package/dist/src/features/tool/use-cases.js +111 -108
- package/dist/src/features/update/latest-version.js +2 -2
- package/dist/src/features/update/update-notification.js +10 -3
- package/dist/src/features/update/update-service.js +57 -23
- package/dist/src/platform/resource-auth-key.js +1 -0
- package/dist/src/platform/signed-http-transport.js +1 -0
- package/dist/src/platform/task-observer.js +1 -0
- package/dist/src/product/billing.js +47 -0
- package/dist/src/product/config.js +85 -0
- package/dist/src/product/locale.js +38 -0
- package/dist/src/product/text.js +3 -0
- package/dist/src/telemetry/sls-sink.js +1 -2
- package/package.json +5 -5
- package/skills/gd-cli/SKILL.md +1 -1
- package/skills/gd-cli/references/agent.md +34 -0
- package/skills/gd-cli/references/creation.md +5 -73
- package/skills/gd-cli/references/errors.md +18 -2
- package/skills/gd-cli/references/generation-constraints.md +36 -0
- package/skills/gd-cli/references/tool.md +31 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { bindAction, bindPreparedAction } from "./action-binding.js";
|
|
2
2
|
import { CliUsageError } from "./errors.js";
|
|
3
|
+
import { productText as text } from "../product/text.js";
|
|
3
4
|
export function registerToolCommands(program, runtime) {
|
|
4
5
|
const tool = program.command("tool")
|
|
5
|
-
.description("创作工具")
|
|
6
|
+
.description(text(runtime.product, "创作工具", "Creation tools"))
|
|
6
7
|
.helpCommand(false);
|
|
7
8
|
const list = tool.command("list")
|
|
8
|
-
.description("列出可用工具")
|
|
9
|
+
.description(text(runtime.product, "列出可用工具", "List available tools"))
|
|
9
10
|
.allowExcessArguments(false);
|
|
10
11
|
bindAction(list, "organization", async (context, signal) => {
|
|
11
12
|
const result = await runtime.tool.listTools({
|
|
@@ -21,11 +22,21 @@ export function registerToolCommands(program, runtime) {
|
|
|
21
22
|
});
|
|
22
23
|
}, { operation: "tool.list" });
|
|
23
24
|
const call = tool.command("call")
|
|
24
|
-
.description("调用创作工具")
|
|
25
|
-
.argument("<tool>", "工具机器标识")
|
|
26
|
-
.option("--schema", "输出当前工具所有模型参数并集的输入 Schema")
|
|
27
|
-
.option("--input <file|->", "从文件或标准输入读取参数 JSON")
|
|
25
|
+
.description(text(runtime.product, "调用创作工具", "Call a creation tool"))
|
|
26
|
+
.argument("<tool>", text(runtime.product, "工具机器标识", "tool machine identifier"))
|
|
27
|
+
.option("--schema", text(runtime.product, "输出当前工具所有模型参数并集的输入 Schema", "output the tool-wide input Schema"))
|
|
28
|
+
.option("--input <file|->", text(runtime.product, "从文件或标准输入读取参数 JSON", "read argument JSON from a file or stdin"))
|
|
29
|
+
.option("--async", text(runtime.product, "提交后返回 taskId,通过 task get 查询结果", "return taskId after submission; retrieve results with task get"))
|
|
28
30
|
.allowExcessArguments(false);
|
|
31
|
+
call.addHelpText("after", [
|
|
32
|
+
"",
|
|
33
|
+
text(runtime.product, "--async 在服务端受理后输出包含 taskId 和 state 的 JSON;退出码 0 仅表示受理。", "--async returns JSON with taskId and state after acceptance; exit code 0 means accepted only."),
|
|
34
|
+
text(runtime.product, "用 task get <taskId> 查询原任务;completed 时 result 为完整结果,running/unknown 或查询超时不应重新提交。", "Query the same taskId with task get; completed includes result. Do not resubmit on running, unknown, or a query timeout."),
|
|
35
|
+
text(runtime.product, "默认等待原 Tool 结果;任务 ID 写入 stderr,stdout 保持原 JSON。", "By default, the command waits for the original Tool result; stderr shows the Task ID and stdout keeps the result JSON."),
|
|
36
|
+
text(runtime.product, `结果未收到时用 ${runtime.product.commandName} task list 找到调用,再用 ${runtime.product.commandName} task get <id> 查询,不重复生成。`, `If output is missing, use ${runtime.product.commandName} task list and ${runtime.product.commandName} task get <id> to recover it without generating again.`),
|
|
37
|
+
text(runtime.product, "查询返回同一份 usage;Tool usage.cost 为当次目录预估价格。", "Queries return the same usage; Tool usage.cost is the catalog price estimate for that invocation."),
|
|
38
|
+
""
|
|
39
|
+
].join("\n"));
|
|
29
40
|
bindPreparedAction(call, "organization", async (signal) => {
|
|
30
41
|
signal.throwIfAborted();
|
|
31
42
|
const name = call.processedArgs[0];
|
|
@@ -33,7 +44,7 @@ export function registerToolCommands(program, runtime) {
|
|
|
33
44
|
throw new CliUsageError();
|
|
34
45
|
const options = call.opts();
|
|
35
46
|
const modes = Number(options.schema === true) + Number(options.input !== undefined);
|
|
36
|
-
if (modes !== 1)
|
|
47
|
+
if (modes !== 1 || (options.schema && options.async))
|
|
37
48
|
throw new CliUsageError();
|
|
38
49
|
if (options.schema === true) {
|
|
39
50
|
return { handled: false, input: { mode: "schema", name } };
|
|
@@ -41,7 +52,7 @@ export function registerToolCommands(program, runtime) {
|
|
|
41
52
|
const argumentsInput = await runtime.jsonInput.read(options.input, signal);
|
|
42
53
|
const input = { name, arguments: argumentsInput };
|
|
43
54
|
runtime.validators.toolCallInput(input);
|
|
44
|
-
return { handled: false, input: { mode: "call", input } };
|
|
55
|
+
return { handled: false, input: { mode: "call", input, async: options.async === true } };
|
|
45
56
|
}, async (context, prepared, signal) => {
|
|
46
57
|
const access = {
|
|
47
58
|
credential: context.state.credential,
|
|
@@ -59,13 +70,26 @@ export function registerToolCommands(program, runtime) {
|
|
|
59
70
|
});
|
|
60
71
|
return;
|
|
61
72
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
73
|
+
const task = {
|
|
74
|
+
state: context.state,
|
|
75
|
+
operation: "tool.call",
|
|
76
|
+
label: `${prepared.input.name}: ${String(prepared.input.arguments.prompt ?? prepared.input.arguments.model ?? "")}`
|
|
77
|
+
};
|
|
78
|
+
if (prepared.async) {
|
|
79
|
+
const result = await runtime.tasks.submit({ ...task,
|
|
80
|
+
run: observe => runtime.tool.submit({ input: prepared.input, access, signal, observe }) });
|
|
81
|
+
runtime.validators.taskResult(result);
|
|
82
|
+
runtime.io.writeOut(`${JSON.stringify(result)}\n`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const run = async (observe) => {
|
|
86
|
+
const result = await runtime.tool.call({ input: prepared.input, access, signal,
|
|
87
|
+
...(observe ? { observe } : {}) });
|
|
68
88
|
runtime.validators.toolCallOutput(result);
|
|
89
|
+
return result;
|
|
90
|
+
};
|
|
91
|
+
const result = await runtime.tasks.track({ ...task, run });
|
|
92
|
+
await runtime.telemetry.stage("output", async () => {
|
|
69
93
|
runtime.io.writeOut(`${JSON.stringify(result)}\n`);
|
|
70
94
|
});
|
|
71
95
|
}, {
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { bindAction } from "./action-binding.js";
|
|
2
|
+
import { productText as text } from "../product/text.js";
|
|
2
3
|
export function registerUpdateCommand(program, runtime) {
|
|
3
4
|
const update = program.command("update")
|
|
4
|
-
.description("更新 GD CLI 与内置 Agent Skill")
|
|
5
|
+
.description(text(runtime.product, "更新 GD CLI 与内置 Agent Skill", "Update insMind CLI and its bundled Agent Skill"))
|
|
5
6
|
.allowExcessArguments(false);
|
|
6
7
|
bindAction(update, "none", async (_context, signal) => {
|
|
7
8
|
const result = await runtime.update.run(signal);
|
|
8
9
|
await runtime.telemetry.stage("output", async () => {
|
|
9
10
|
runtime.presenter.success(result.updated
|
|
10
|
-
? `gd-cli 已更新到 ${result.version},Agent Skill
|
|
11
|
-
: `gd-cli ${result.version} 已是最新版本,Agent Skill
|
|
11
|
+
? text(runtime.product, `gd-cli 已更新到 ${result.version},Agent Skill 已同步。`, `insmind-cli updated to ${result.version}; Agent Skill synchronized.`)
|
|
12
|
+
: text(runtime.product, `gd-cli ${result.version} 已是最新版本,Agent Skill 已同步。`, `insmind-cli ${result.version} is current; Agent Skill synchronized.`));
|
|
12
13
|
});
|
|
13
14
|
}, { operation: "update" });
|
|
14
15
|
}
|
|
@@ -2,7 +2,7 @@ import { buildCreativeRequest, creativeContextId, creativeTurnMessages, normaliz
|
|
|
2
2
|
import { parseCreativeStream } from "./creative-stream.js";
|
|
3
3
|
export function createCreativeAgentAdapter(dependencies) {
|
|
4
4
|
return {
|
|
5
|
-
async complete(message, access, signal) {
|
|
5
|
+
async complete(message, access, signal, observe) {
|
|
6
6
|
signal.throwIfAborted();
|
|
7
7
|
const ids = message.contextId === undefined
|
|
8
8
|
? {
|
|
@@ -10,7 +10,9 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
10
10
|
localMessageId: dependencies.idFactory()
|
|
11
11
|
}
|
|
12
12
|
: { localMessageId: dependencies.idFactory() };
|
|
13
|
+
await observe?.({ localMessageId: ids.localMessageId, ...(message.contextId ? { contextId: message.contextId } : {}) });
|
|
13
14
|
const streamed = await dependencies.telemetry.stage("execute", async () => {
|
|
15
|
+
await observe?.({ submissionStarted: true });
|
|
14
16
|
const stream = await dependencies.transport.postStream({
|
|
15
17
|
path: "/ai-agent/v1/thread/completion",
|
|
16
18
|
body: buildCreativeRequest(message, ids),
|
|
@@ -19,7 +21,8 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
19
21
|
organizationId: access.organizationId,
|
|
20
22
|
signal
|
|
21
23
|
});
|
|
22
|
-
|
|
24
|
+
await observe?.({ accepted: true });
|
|
25
|
+
return parseCreativeStream(stream, signal, observe ? async (contextId) => observe({ contextId }) : undefined);
|
|
23
26
|
});
|
|
24
27
|
const contextId = creativeContextId(streamed);
|
|
25
28
|
return dependencies.telemetry.stage("result", async () => {
|
|
@@ -29,7 +32,10 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
29
32
|
organizationId: access.organizationId,
|
|
30
33
|
signal
|
|
31
34
|
});
|
|
32
|
-
|
|
35
|
+
const output = normalizeCreativeMessages(restoreCreativeResourceUrls(creativeTurnMessages(messages, ids.localMessageId), streamed));
|
|
36
|
+
return dependencies.resourceAuthorizer
|
|
37
|
+
? dependencies.resourceAuthorizer.authorize(output, access, signal)
|
|
38
|
+
: output;
|
|
33
39
|
});
|
|
34
40
|
}
|
|
35
41
|
};
|
|
@@ -127,7 +127,11 @@ export function creativeTurnMessages(value, localMessageId) {
|
|
|
127
127
|
});
|
|
128
128
|
if (start < 0)
|
|
129
129
|
return fail();
|
|
130
|
-
|
|
130
|
+
const nextInput = messages.findIndex((message, index) => index > start
|
|
131
|
+
&& typeof message.local_message_id === "string" && message.local_message_id !== ""
|
|
132
|
+
&& message.local_message_id !== localMessageId
|
|
133
|
+
&& (message.role === "user" || contentOf(message)?.type === "function_response"));
|
|
134
|
+
return messages.slice(start, nextInput < 0 ? undefined : nextInput);
|
|
131
135
|
}
|
|
132
136
|
function contentOf(message) {
|
|
133
137
|
return record(message.content);
|
|
@@ -209,14 +213,6 @@ function resourceUrlIdentity(url) {
|
|
|
209
213
|
return undefined;
|
|
210
214
|
}
|
|
211
215
|
}
|
|
212
|
-
function requiresAuthKey(url) {
|
|
213
|
-
try {
|
|
214
|
-
return /^gd-ai-[a-z0-9-]+\.dancf\.com$/iu.test(new URL(url).hostname);
|
|
215
|
-
}
|
|
216
|
-
catch {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
216
|
export function restoreCreativeResourceUrls(messages, streamed) {
|
|
221
217
|
const signedUrls = new Map();
|
|
222
218
|
for (const message of streamed) {
|
|
@@ -290,13 +286,6 @@ export function restoreCreativeResourceUrls(messages, streamed) {
|
|
|
290
286
|
}
|
|
291
287
|
};
|
|
292
288
|
});
|
|
293
|
-
for (const message of restored) {
|
|
294
|
-
const content = contentOf(message);
|
|
295
|
-
if (content?.type !== "function_response")
|
|
296
|
-
continue;
|
|
297
|
-
if (functionResponseParts(content.text).some((part) => ("url" in part && requiresAuthKey(part.url) && !hasAuthKey(part.url))))
|
|
298
|
-
return fail();
|
|
299
|
-
}
|
|
300
289
|
return restored;
|
|
301
290
|
}
|
|
302
291
|
function parseQuestionPart(message) {
|
|
@@ -86,11 +86,12 @@ function parseSequence(payload) {
|
|
|
86
86
|
}
|
|
87
87
|
return values;
|
|
88
88
|
}
|
|
89
|
-
export async function parseCreativeStream(stream, signal) {
|
|
89
|
+
export async function parseCreativeStream(stream, signal, onContextId) {
|
|
90
90
|
signal.throwIfAborted();
|
|
91
91
|
const reader = stream.getReader();
|
|
92
92
|
const decoder = new TextDecoder();
|
|
93
93
|
let raw = "";
|
|
94
|
+
let contextReported = false;
|
|
94
95
|
const onAbort = () => {
|
|
95
96
|
void reader.cancel(signal.reason).catch(() => undefined);
|
|
96
97
|
};
|
|
@@ -103,6 +104,13 @@ export async function parseCreativeStream(stream, signal) {
|
|
|
103
104
|
if (result.done)
|
|
104
105
|
break;
|
|
105
106
|
raw += decoder.decode(result.value, { stream: true });
|
|
107
|
+
if (onContextId && !contextReported) {
|
|
108
|
+
const contextId = messagesFrom(raw).find(message => typeof message.thread_id === "string" && message.thread_id)?.thread_id;
|
|
109
|
+
if (contextId) {
|
|
110
|
+
await onContextId(contextId);
|
|
111
|
+
contextReported = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
106
114
|
}
|
|
107
115
|
raw += decoder.decode();
|
|
108
116
|
}
|
|
@@ -110,6 +118,9 @@ export async function parseCreativeStream(stream, signal) {
|
|
|
110
118
|
signal.removeEventListener("abort", onAbort);
|
|
111
119
|
reader.releaseLock();
|
|
112
120
|
}
|
|
121
|
+
return messagesFrom(raw);
|
|
122
|
+
}
|
|
123
|
+
function messagesFrom(raw) {
|
|
113
124
|
const messages = [];
|
|
114
125
|
for (const payload of extractPayloads(raw)) {
|
|
115
126
|
for (const value of parseSequence(payload)) {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RemoteProtocolError } from "../../platform/remote-protocol.js";
|
|
2
|
+
function isRecord(value) {
|
|
3
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
export function createFilemsResourceAuthKeyProvider(dependencies) {
|
|
6
|
+
return {
|
|
7
|
+
async getAuthKey(access, signal) {
|
|
8
|
+
signal.throwIfAborted();
|
|
9
|
+
const response = await dependencies.transport.getJson({
|
|
10
|
+
path: "/filems/access/token",
|
|
11
|
+
credential: access.credential,
|
|
12
|
+
organizationId: access.organizationId,
|
|
13
|
+
signal
|
|
14
|
+
});
|
|
15
|
+
signal.throwIfAborted();
|
|
16
|
+
if (!isRecord(response) || typeof response.auth_key !== "string"
|
|
17
|
+
|| ["", "null", "undefined"].includes(response.auth_key.trim())) {
|
|
18
|
+
throw new RemoteProtocolError();
|
|
19
|
+
}
|
|
20
|
+
return response.auth_key;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { RemoteProtocolError } from "../../platform/remote-protocol.js";
|
|
2
|
+
function unsignedPrivateUrl(value) {
|
|
3
|
+
let url;
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(value);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
throw new RemoteProtocolError();
|
|
9
|
+
}
|
|
10
|
+
const isPrivate = /^gd-ai-[a-z0-9-]+\.dancf\.com$/iu.test(url.hostname)
|
|
11
|
+
|| url.hostname === "dify-grsai.dancf.com"
|
|
12
|
+
|| url.hostname === "dify-aiznt.dancf.com";
|
|
13
|
+
if (!isPrivate || url.searchParams.get("auth_key")?.trim())
|
|
14
|
+
return undefined;
|
|
15
|
+
// Only send FileMS credentials to the verified HTTPS resource hosts.
|
|
16
|
+
if (url.protocol !== "https:" || url.username !== "" || url.password !== ""
|
|
17
|
+
|| url.port !== "")
|
|
18
|
+
throw new RemoteProtocolError();
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
export function createAgentResourceAuthorizer(dependencies) {
|
|
22
|
+
return {
|
|
23
|
+
async authorize(output, access, signal) {
|
|
24
|
+
signal.throwIfAborted();
|
|
25
|
+
const urls = output.message.parts.map((part) => ("url" in part ? unsignedPrivateUrl(part.url) : undefined));
|
|
26
|
+
if (urls.every((url) => url === undefined))
|
|
27
|
+
return output;
|
|
28
|
+
// One token serves this result only; do not retain it across identities or commands.
|
|
29
|
+
const authKey = await dependencies.authKeys.getAuthKey(access, signal);
|
|
30
|
+
signal.throwIfAborted();
|
|
31
|
+
const parts = output.message.parts.map((part, index) => {
|
|
32
|
+
const url = urls[index];
|
|
33
|
+
if (url === undefined || !("url" in part))
|
|
34
|
+
return part;
|
|
35
|
+
url.searchParams.set("auth_key", authKey);
|
|
36
|
+
return { ...part, url: url.href };
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
...output,
|
|
40
|
+
message: {
|
|
41
|
+
...output.message,
|
|
42
|
+
parts: [parts[0], ...parts.slice(1)]
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RemoteProtocolError } from "../../platform/remote-protocol.js";
|
|
2
|
+
import { creativeTurnMessages, normalizeCreativeMessages } from "./creative-protocol.js";
|
|
3
|
+
/** History is a snapshot, not proof that this particular invocation finished. */
|
|
4
|
+
export async function readAgentResult(transport, request) {
|
|
5
|
+
const path = `/ai-agent/v1/thread/${encodeURIComponent(request.contextId)}`;
|
|
6
|
+
const access = { credential: request.credential, signal: request.signal,
|
|
7
|
+
...(request.organizationId === undefined ? {} : { organizationId: request.organizationId }) };
|
|
8
|
+
const messages = await transport.getJson({ ...access, path: `${path}/messages` });
|
|
9
|
+
const connection = await transport.getJson({ ...access, path: `${path}/connection` });
|
|
10
|
+
if (!connection || typeof connection !== "object" || !("state" in connection)
|
|
11
|
+
|| typeof connection.state !== "string"
|
|
12
|
+
|| !["running", "stopping", "inactive"].includes(connection.state))
|
|
13
|
+
throw new RemoteProtocolError();
|
|
14
|
+
if (!Array.isArray(messages))
|
|
15
|
+
throw new RemoteProtocolError();
|
|
16
|
+
if (!messages.some(m => m && typeof m === "object" && m.local_message_id === request.localMessageId)) {
|
|
17
|
+
return { connectionState: connection.state };
|
|
18
|
+
}
|
|
19
|
+
const turn = creativeTurnMessages(messages, request.localMessageId);
|
|
20
|
+
if (!turn.some(m => m.role !== "user" && m.content))
|
|
21
|
+
return { connectionState: connection.state };
|
|
22
|
+
try {
|
|
23
|
+
return { connectionState: connection.state, partialResult: normalizeCreativeMessages(turn) };
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error instanceof RemoteProtocolError)
|
|
27
|
+
return { connectionState: connection.state };
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -35,116 +35,119 @@ function inferMediaType(filename) {
|
|
|
35
35
|
: mediaTypes[extension] ?? "application/octet-stream";
|
|
36
36
|
}
|
|
37
37
|
export function createAgentUseCases(dependencies) {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
async function prepare({ input, access, signal }) {
|
|
39
|
+
signal.throwIfAborted();
|
|
40
|
+
const prepared = [];
|
|
41
|
+
let generation;
|
|
42
|
+
for (const part of input.message.parts) {
|
|
43
|
+
if ("data" in part && !("questionMessageId" in part.data)) {
|
|
44
|
+
generation = part.data;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (generation?.model !== undefined) {
|
|
49
|
+
dependencies.telemetry.annotate({ model_id: generation.model });
|
|
50
|
+
const catalog = await dependencies.telemetry.stage("catalog", () => (dependencies.catalog.load({ access, signal })));
|
|
40
51
|
signal.throwIfAborted();
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
52
|
+
for (const warning of catalog.warnings)
|
|
53
|
+
dependencies.warn(warning);
|
|
54
|
+
await dependencies.telemetry.stage("input", async () => {
|
|
55
|
+
let model;
|
|
56
|
+
try {
|
|
57
|
+
model = findModel(catalog, generation.model);
|
|
47
58
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof ToolInputError)
|
|
61
|
+
throw new AgentInputError();
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (!modelControlArgumentsAreValid(model, generation.arguments ?? {}))
|
|
65
|
+
throw new AgentInputError();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const prepare = async () => {
|
|
69
|
+
for (const part of input.message.parts) {
|
|
52
70
|
signal.throwIfAborted();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
71
|
+
if ("text" in part) {
|
|
72
|
+
prepared.push({ kind: "text", text: part.text });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if ("url" in part) {
|
|
76
|
+
const role = part.metadata?.role ?? "reference";
|
|
77
|
+
if (part.url.startsWith("file:")) {
|
|
78
|
+
const uploadInput = {
|
|
79
|
+
url: part.url,
|
|
80
|
+
...(part.mediaType === undefined ? {} : { mediaType: part.mediaType }),
|
|
81
|
+
...(part.filename === undefined ? {} : { filename: part.filename })
|
|
82
|
+
};
|
|
83
|
+
const asset = await dependencies.uploader.upload(uploadInput, access, signal);
|
|
84
|
+
prepared.push({ kind: "asset", asset, role });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
let url;
|
|
57
88
|
try {
|
|
58
|
-
|
|
89
|
+
url = parseSafeRemoteAssetUrl(part.url);
|
|
59
90
|
}
|
|
60
91
|
catch (error) {
|
|
61
|
-
if (error instanceof
|
|
92
|
+
if (error instanceof UrlSafetyError)
|
|
62
93
|
throw new AgentInputError();
|
|
63
94
|
throw error;
|
|
64
95
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const uploadInput = {
|
|
80
|
-
url: part.url,
|
|
81
|
-
...(part.mediaType === undefined ? {} : { mediaType: part.mediaType }),
|
|
82
|
-
...(part.filename === undefined ? {} : { filename: part.filename })
|
|
83
|
-
};
|
|
84
|
-
const asset = await dependencies.uploader.upload(uploadInput, access, signal);
|
|
85
|
-
prepared.push({ kind: "asset", asset, role });
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
let url;
|
|
89
|
-
try {
|
|
90
|
-
url = parseSafeRemoteAssetUrl(part.url);
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
if (error instanceof UrlSafetyError)
|
|
94
|
-
throw new AgentInputError();
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
const filename = part.filename ?? pathnameFilename(url);
|
|
98
|
-
prepared.push({
|
|
99
|
-
kind: "asset",
|
|
100
|
-
asset: {
|
|
101
|
-
url: part.url,
|
|
102
|
-
mediaType: part.mediaType ?? inferMediaType(filename),
|
|
103
|
-
filename,
|
|
104
|
-
fromUserUpload: false
|
|
105
|
-
},
|
|
106
|
-
role
|
|
107
|
-
});
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
if ("questionMessageId" in part.data) {
|
|
111
|
-
prepared.push({
|
|
112
|
-
kind: "answers",
|
|
113
|
-
data: {
|
|
114
|
-
questionMessageId: part.data.questionMessageId,
|
|
115
|
-
answers: part.data.answers.map((answer) => ({
|
|
116
|
-
question: answer.question,
|
|
117
|
-
values: [...answer.values]
|
|
118
|
-
}))
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
96
|
+
const filename = part.filename ?? pathnameFilename(url);
|
|
97
|
+
prepared.push({
|
|
98
|
+
kind: "asset",
|
|
99
|
+
asset: {
|
|
100
|
+
url: part.url,
|
|
101
|
+
mediaType: part.mediaType ?? inferMediaType(filename),
|
|
102
|
+
filename,
|
|
103
|
+
fromUserUpload: false
|
|
104
|
+
},
|
|
105
|
+
role
|
|
106
|
+
});
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if ("questionMessageId" in part.data) {
|
|
123
110
|
prepared.push({
|
|
124
|
-
kind: "
|
|
111
|
+
kind: "answers",
|
|
125
112
|
data: {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
:
|
|
113
|
+
questionMessageId: part.data.questionMessageId,
|
|
114
|
+
answers: part.data.answers.map((answer) => ({
|
|
115
|
+
question: answer.question,
|
|
116
|
+
values: [...answer.values]
|
|
117
|
+
}))
|
|
130
118
|
}
|
|
131
119
|
});
|
|
120
|
+
continue;
|
|
132
121
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
122
|
+
prepared.push({
|
|
123
|
+
kind: "generation",
|
|
124
|
+
data: {
|
|
125
|
+
...(part.data.model === undefined ? {} : { model: part.data.model }),
|
|
126
|
+
...(part.data.arguments === undefined
|
|
127
|
+
? {}
|
|
128
|
+
: { arguments: { ...part.data.arguments } })
|
|
129
|
+
}
|
|
130
|
+
});
|
|
140
131
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
132
|
+
};
|
|
133
|
+
if (input.message.parts.some((part) => (typeof part === "object" && part !== null
|
|
134
|
+
&& "url" in part && typeof part.url === "string" && part.url.startsWith("file:")))) {
|
|
135
|
+
await dependencies.telemetry.stage("upload", prepare);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
await prepare();
|
|
139
|
+
}
|
|
140
|
+
signal.throwIfAborted();
|
|
141
|
+
return {
|
|
142
|
+
...(input.message.contextId === undefined
|
|
143
|
+
? {}
|
|
144
|
+
: { contextId: input.message.contextId }),
|
|
145
|
+
parts: prepared
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
async send(input) {
|
|
150
|
+
return dependencies.completion.complete(await prepare(input), input.access, input.signal, ...(input.observe ? [input.observe] : []));
|
|
148
151
|
}
|
|
149
152
|
};
|
|
150
153
|
}
|
|
@@ -4,7 +4,7 @@ import { createLocalJsonFile, LocalFileError } from "../../platform/local-json-f
|
|
|
4
4
|
import { AuthStateInvalidError, parseStoredAuthState } from "./state.js";
|
|
5
5
|
export function createLocalCredentialStore(options = {}) {
|
|
6
6
|
const file = options.file ?? createLocalJsonFile({
|
|
7
|
-
directory: join(options.homeDirectory ?? homedir(), ".gd"),
|
|
7
|
+
directory: join(options.homeDirectory ?? homedir(), options.stateDirectory ?? ".gd"),
|
|
8
8
|
filename: "credentials.json"
|
|
9
9
|
});
|
|
10
10
|
return {
|
|
@@ -44,7 +44,9 @@ export function createSsoService(options) {
|
|
|
44
44
|
}
|
|
45
45
|
return {
|
|
46
46
|
async startDeviceAuthorization(signal) {
|
|
47
|
-
const response = await request(
|
|
47
|
+
const response = await request(`oauth/device-authorization?${new URLSearchParams({
|
|
48
|
+
clientId: options.clientId ?? "gdhub-cli"
|
|
49
|
+
})}`, signal);
|
|
48
50
|
if (!response.ok)
|
|
49
51
|
throw new SsoRequestError(response.status);
|
|
50
52
|
const body = await responseRecord(response);
|