@agentstep/agent-sdk 0.5.33 → 0.5.35
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/{chunk-3B4JRSYA.js → chunk-25E64UIV.js} +1 -1
- package/dist/{chunk-SHUFUWAB.js → chunk-2R4IT7HG.js} +2 -2
- package/dist/{chunk-NLJK7FEN.js → chunk-3EI7IPMI.js} +2 -2
- package/dist/{chunk-EUMA5Q4U.js → chunk-3EWEKBVC.js} +1 -1
- package/dist/{chunk-USSUE7J2.js → chunk-3LYNN5VT.js} +1 -1
- package/dist/{chunk-XOWRUT4X.js → chunk-4EKHW6VS.js} +1 -1
- package/dist/{chunk-KSL2D4AD.js → chunk-6CODFATQ.js} +1 -1
- package/dist/{chunk-J7VBHBXL.js → chunk-6NEWWPL4.js} +1 -1
- package/dist/{chunk-Y5RNFM44.js → chunk-6NFK5RJX.js} +1 -1
- package/dist/{chunk-7JA6HCMK.js → chunk-75US4UAE.js} +1 -1
- package/dist/{chunk-AK6HMO7I.js → chunk-AD2WPGDN.js} +1 -1
- package/dist/{chunk-SDTRWSGF.js → chunk-BSAF4YJB.js} +3 -3
- package/dist/{chunk-TKFAWQD7.js → chunk-CZ4N5FJW.js} +1 -1
- package/dist/chunk-DI6IMZ3P.js +0 -0
- package/dist/{chunk-TTZGQIQS.js → chunk-FCPZF7KJ.js} +1 -1
- package/dist/{chunk-CHNJK2KW.js → chunk-FHOJOLXU.js} +1 -1
- package/dist/{chunk-BKMY6TSV.js → chunk-GABMXDZZ.js} +1 -1
- package/dist/{chunk-ZV5Y5JBE.js → chunk-GBBQD6VY.js} +1 -1
- package/dist/{chunk-CWVYFBZF.js → chunk-GFZ2WIVH.js} +1 -1
- package/dist/{chunk-JMDV55BV.js → chunk-HHLVVJGT.js} +1 -1
- package/dist/chunk-IYE5USSW.js +412 -0
- package/dist/{chunk-QGPHATO3.js → chunk-JDAOTVWV.js} +1 -1
- package/dist/{chunk-C3UXUDZS.js → chunk-JIXRTWYL.js} +1 -1
- package/dist/{chunk-T3TNJHED.js → chunk-K3LM6O44.js} +2 -2
- package/dist/{chunk-YRFWPBGX.js → chunk-K5J4IN2S.js} +1 -1
- package/dist/{chunk-AKGWEACL.js → chunk-KYKVHH7I.js} +2 -2
- package/dist/{chunk-7PFDF5PN.js → chunk-LKHJLSYG.js} +3 -3
- package/dist/{chunk-B24Q4CUC.js → chunk-LR6FK2J7.js} +1 -1
- package/dist/{chunk-ZPKQT6X2.js → chunk-NQ6BN7WH.js} +1 -1
- package/dist/{chunk-DAOKOXGY.js → chunk-OEYLPPID.js} +2 -2
- package/dist/{chunk-N7XSXI5O.js → chunk-P7V3S2T3.js} +5 -5
- package/dist/chunk-QZE37OGX.js +164 -0
- package/dist/{chunk-UNO3TSAT.js → chunk-SUEPFZN2.js} +1 -1
- package/dist/{chunk-6RRK27I3.js → chunk-VQM2MCQR.js} +1 -1
- package/dist/chunk-X7UWCDYG.js +99 -0
- package/dist/{chunk-CJIGDJIJ.js → chunk-XXAO5YYL.js} +1 -1
- package/dist/{chunk-S6HILC3F.js → chunk-ZACPJA3G.js} +1 -1
- package/dist/{chunk-JNLVQGSH.js → chunk-ZAUWB5DQ.js} +1 -1
- package/dist/{chunk-U4SVWPLC.js → chunk-ZCCHLDLC.js} +1 -1
- package/dist/{chunk-KLGAE7V4.js → chunk-ZHEE6UTX.js} +1 -1
- package/dist/containers/lifecycle.js +2 -2
- package/dist/containers/setup.js +2 -2
- package/dist/handlers/agents.js +8 -8
- package/dist/handlers/api_keys.js +8 -8
- package/dist/handlers/audit.js +8 -8
- package/dist/handlers/batch.js +8 -8
- package/dist/handlers/credentials.js +9 -9
- package/dist/handlers/enrollment.js +8 -8
- package/dist/handlers/environments.js +9 -9
- package/dist/handlers/events.js +8 -8
- package/dist/handlers/files.js +8 -8
- package/dist/handlers/google-compat/agents.js +106 -0
- package/dist/handlers/google-compat/files.js +101 -0
- package/dist/handlers/google-compat/index.js +122 -0
- package/dist/handlers/google-compat/interactions.js +106 -0
- package/dist/handlers/google-compat/types.js +0 -0
- package/dist/handlers/index.js +61 -36
- package/dist/handlers/license.js +8 -8
- package/dist/handlers/memory.js +8 -8
- package/dist/handlers/metrics.js +8 -8
- package/dist/handlers/models.js +8 -8
- package/dist/handlers/providers.js +8 -8
- package/dist/handlers/resources.js +8 -8
- package/dist/handlers/sessions.js +9 -9
- package/dist/handlers/settings.js +8 -8
- package/dist/handlers/skills-write.js +8 -8
- package/dist/handlers/skills.js +8 -8
- package/dist/handlers/stream.js +7 -7
- package/dist/handlers/tenants.js +8 -8
- package/dist/handlers/threads.js +8 -8
- package/dist/handlers/traces.js +8 -8
- package/dist/handlers/upstream_keys.js +8 -8
- package/dist/handlers/user-profiles.js +8 -8
- package/dist/handlers/vaults.js +8 -8
- package/dist/handlers/whoami.js +8 -8
- package/dist/handlers/work.js +8 -8
- package/dist/http.js +7 -7
- package/dist/index.js +9 -9
- package/dist/init.js +6 -6
- package/dist/providers/registry.js +1 -1
- package/dist/providers/vercel.js +1 -1
- package/dist/sessions/driver.js +3 -3
- package/dist/sessions/sweeper.js +3 -3
- package/dist/sessions/threads.js +3 -3
- package/dist/shutdown.js +4 -4
- package/dist/workers/runner.js +4 -4
- package/package.json +1 -1
- package/dist/{chunk-C7P2TYOG.js → chunk-XLMNSDUJ.js} +0 -0
- package/dist/{dist-3ZD3ELTH.js → dist-EY25RQ2S.js} +3 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
loadVaultForCaller
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-GFZ2WIVH.js";
|
|
4
4
|
import {
|
|
5
5
|
jsonOk,
|
|
6
6
|
paginatedOk,
|
|
7
7
|
routeWrap
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-4EKHW6VS.js";
|
|
9
9
|
import {
|
|
10
10
|
archiveCredential,
|
|
11
11
|
createCredential,
|
|
@@ -17,13 +17,13 @@ import {
|
|
|
17
17
|
} from "./chunk-TVV7AE3G.js";
|
|
18
18
|
import {
|
|
19
19
|
kickoffEnvironmentSetup
|
|
20
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-K5J4IN2S.js";
|
|
21
21
|
import {
|
|
22
22
|
decodeCursor,
|
|
23
23
|
jsonOk,
|
|
24
24
|
paginatedOk,
|
|
25
25
|
routeWrap
|
|
26
|
-
} from "./chunk-
|
|
26
|
+
} from "./chunk-4EKHW6VS.js";
|
|
27
27
|
import {
|
|
28
28
|
forwardToAnthropic
|
|
29
29
|
} from "./chunk-JFHYXFAL.js";
|
|
@@ -43,7 +43,7 @@ import {
|
|
|
43
43
|
} from "./chunk-LAWTTG2E.js";
|
|
44
44
|
import {
|
|
45
45
|
releaseSession
|
|
46
|
-
} from "./chunk-
|
|
46
|
+
} from "./chunk-ZCCHLDLC.js";
|
|
47
47
|
import {
|
|
48
48
|
appendEvent,
|
|
49
49
|
dropEmitter
|
|
File without changes
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import {
|
|
2
|
+
jsonOk,
|
|
3
|
+
routeWrap
|
|
4
|
+
} from "./chunk-4EKHW6VS.js";
|
|
5
|
+
import {
|
|
6
|
+
init_ids,
|
|
7
|
+
newId
|
|
8
|
+
} from "./chunk-F4WUVOLE.js";
|
|
9
|
+
import {
|
|
10
|
+
getDb,
|
|
11
|
+
init_client
|
|
12
|
+
} from "./chunk-AGIXZFHQ.js";
|
|
13
|
+
import {
|
|
14
|
+
badRequest,
|
|
15
|
+
notFound
|
|
16
|
+
} from "./chunk-EZYKRG4W.js";
|
|
17
|
+
|
|
18
|
+
// src/handlers/google-compat/interactions.ts
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
init_client();
|
|
21
|
+
init_ids();
|
|
22
|
+
function ensureTable() {
|
|
23
|
+
const db = getDb();
|
|
24
|
+
db.exec(`
|
|
25
|
+
CREATE TABLE IF NOT EXISTS google_interactions (
|
|
26
|
+
id TEXT PRIMARY KEY,
|
|
27
|
+
session_id TEXT NOT NULL,
|
|
28
|
+
seq INTEGER NOT NULL DEFAULT 1,
|
|
29
|
+
status TEXT NOT NULL DEFAULT 'completed',
|
|
30
|
+
environment_id TEXT,
|
|
31
|
+
created_at TEXT NOT NULL
|
|
32
|
+
)
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
var CreateSchema = z.object({
|
|
36
|
+
model: z.string().optional(),
|
|
37
|
+
agent: z.string().optional(),
|
|
38
|
+
input: z.union([z.string(), z.array(z.unknown())]),
|
|
39
|
+
system_instruction: z.string().optional(),
|
|
40
|
+
tools: z.array(z.unknown()).optional(),
|
|
41
|
+
generation_config: z.record(z.unknown()).optional(),
|
|
42
|
+
previous_interaction_id: z.string().optional(),
|
|
43
|
+
environment: z.union([z.string(), z.object({ type: z.string() }).passthrough()]).optional(),
|
|
44
|
+
stream: z.boolean().optional()
|
|
45
|
+
});
|
|
46
|
+
function handleCreateInteraction(request) {
|
|
47
|
+
return routeWrap(request, async ({ auth }) => {
|
|
48
|
+
ensureTable();
|
|
49
|
+
const body = await request.json().catch(() => null);
|
|
50
|
+
const parsed = CreateSchema.safeParse(body);
|
|
51
|
+
if (!parsed.success) {
|
|
52
|
+
throw badRequest(`invalid body: ${parsed.error.issues.map((i) => i.message).join("; ")}`);
|
|
53
|
+
}
|
|
54
|
+
const data = parsed.data;
|
|
55
|
+
if (!data.model && !data.agent) {
|
|
56
|
+
throw badRequest("either 'model' or 'agent' is required");
|
|
57
|
+
}
|
|
58
|
+
const inputText = typeof data.input === "string" ? data.input : JSON.stringify(data.input);
|
|
59
|
+
const { handleCreateAgent, handleListAgents } = await import("./handlers/agents.js");
|
|
60
|
+
const { handleCreateSession } = await import("./handlers/sessions.js");
|
|
61
|
+
const { handlePostEvents } = await import("./handlers/events.js");
|
|
62
|
+
let agentId;
|
|
63
|
+
let environmentId;
|
|
64
|
+
if (data.previous_interaction_id) {
|
|
65
|
+
const db2 = getDb();
|
|
66
|
+
const prev = db2.prepare(
|
|
67
|
+
`SELECT session_id, environment_id FROM google_interactions WHERE id = ?`
|
|
68
|
+
).get(data.previous_interaction_id);
|
|
69
|
+
if (!prev) throw notFound(`interaction not found: ${data.previous_interaction_id}`);
|
|
70
|
+
agentId = "";
|
|
71
|
+
environmentId = prev.environment_id ?? void 0;
|
|
72
|
+
const sessionId2 = prev.session_id;
|
|
73
|
+
const { listEvents: listEventsForSeq } = await import("./db/events.js");
|
|
74
|
+
const lastEvents = listEventsForSeq(sessionId2, { limit: 1, order: "desc" });
|
|
75
|
+
const afterSeq = lastEvents.length > 0 ? lastEvents[0].seq : 0;
|
|
76
|
+
const functionResultEvents = buildFunctionResultEvents(data.input);
|
|
77
|
+
const eventsReq2 = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/sessions/${sessionId2}/events`), {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: request.headers,
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
events: functionResultEvents.length > 0 ? functionResultEvents : [{ type: "user.message", content: [{ type: "text", text: inputText }] }]
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
const eventsRes2 = await handlePostEvents(eventsReq2, sessionId2);
|
|
85
|
+
if (!eventsRes2.ok) {
|
|
86
|
+
const err = await eventsRes2.json().catch(() => ({}));
|
|
87
|
+
throw badRequest(err.error?.message || `failed to send message: ${eventsRes2.status}`);
|
|
88
|
+
}
|
|
89
|
+
const result2 = await waitForCompletion(sessionId2, afterSeq);
|
|
90
|
+
const interactionId2 = `int_${newId("sesn").slice(5)}`;
|
|
91
|
+
const prevSeq = db2.prepare(
|
|
92
|
+
`SELECT MAX(seq) as maxSeq FROM google_interactions WHERE session_id = ?`
|
|
93
|
+
).get(sessionId2);
|
|
94
|
+
const seq = (prevSeq?.maxSeq ?? 0) + 1;
|
|
95
|
+
db2.prepare(
|
|
96
|
+
`INSERT INTO google_interactions (id, session_id, seq, status, environment_id, created_at) VALUES (?, ?, ?, ?, ?, ?)`
|
|
97
|
+
).run(interactionId2, sessionId2, seq, result2.status, environmentId ?? null, (/* @__PURE__ */ new Date()).toISOString());
|
|
98
|
+
return jsonOk(buildResponse(interactionId2, result2, environmentId));
|
|
99
|
+
}
|
|
100
|
+
const agentName = data.agent || `auto-${(data.model || "gemini-2.5-flash").replace(/[^a-z0-9-]/g, "-")}`;
|
|
101
|
+
const listReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/agents?limit=1000`), {
|
|
102
|
+
headers: request.headers
|
|
103
|
+
});
|
|
104
|
+
const listRes = await handleListAgents(listReq);
|
|
105
|
+
const listBody = await listRes.json();
|
|
106
|
+
const existing = listBody.data?.find((a) => a.name === agentName);
|
|
107
|
+
if (existing) {
|
|
108
|
+
agentId = existing.id;
|
|
109
|
+
} else {
|
|
110
|
+
const modelId = data.model || "gemini-2.5-flash";
|
|
111
|
+
const createReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/agents`), {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: request.headers,
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
name: agentName,
|
|
116
|
+
model: { id: modelId },
|
|
117
|
+
system: data.system_instruction ?? null
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
const createRes = await handleCreateAgent(createReq);
|
|
121
|
+
if (!createRes.ok) {
|
|
122
|
+
const err = await createRes.json().catch(() => ({}));
|
|
123
|
+
throw badRequest(err.error?.message || `failed to create agent: ${createRes.status}`);
|
|
124
|
+
}
|
|
125
|
+
const created = await createRes.json();
|
|
126
|
+
agentId = created.id;
|
|
127
|
+
}
|
|
128
|
+
const { listEnvironments } = await import("./db/environments.js");
|
|
129
|
+
const envs = listEnvironments({ includeArchived: false, limit: 10 });
|
|
130
|
+
const readyEnv = envs.find((e) => e.state === "ready");
|
|
131
|
+
if (readyEnv) {
|
|
132
|
+
environmentId = readyEnv.id;
|
|
133
|
+
} else {
|
|
134
|
+
const { handleCreateEnvironment } = await import("./handlers/environments.js");
|
|
135
|
+
const envReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/environments`), {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers: request.headers,
|
|
138
|
+
body: JSON.stringify({ name: `google-compat-${Date.now()}` })
|
|
139
|
+
});
|
|
140
|
+
const envRes = await handleCreateEnvironment(envReq);
|
|
141
|
+
if (envRes.ok) {
|
|
142
|
+
const envBody = await envRes.json();
|
|
143
|
+
environmentId = envBody.id;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!environmentId) throw badRequest("no environment available");
|
|
147
|
+
const sessReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/sessions`), {
|
|
148
|
+
method: "POST",
|
|
149
|
+
headers: request.headers,
|
|
150
|
+
body: JSON.stringify({
|
|
151
|
+
agent: agentId,
|
|
152
|
+
environment_id: environmentId
|
|
153
|
+
})
|
|
154
|
+
});
|
|
155
|
+
const sessRes = await handleCreateSession(sessReq);
|
|
156
|
+
if (!sessRes.ok) {
|
|
157
|
+
const err = await sessRes.json().catch(() => ({}));
|
|
158
|
+
throw badRequest(err.error?.message || `failed to create session: ${sessRes.status}`);
|
|
159
|
+
}
|
|
160
|
+
const sessBody = await sessRes.json();
|
|
161
|
+
const sessionId = sessBody.id;
|
|
162
|
+
const eventsReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/sessions/${sessionId}/events`), {
|
|
163
|
+
method: "POST",
|
|
164
|
+
headers: request.headers,
|
|
165
|
+
body: JSON.stringify({
|
|
166
|
+
events: [{ type: "user.message", content: [{ type: "text", text: inputText }] }]
|
|
167
|
+
})
|
|
168
|
+
});
|
|
169
|
+
const eventsRes = await handlePostEvents(eventsReq, sessionId);
|
|
170
|
+
if (!eventsRes.ok) {
|
|
171
|
+
const err = await eventsRes.json().catch(() => ({}));
|
|
172
|
+
throw badRequest(err.error?.message || `failed to send message: ${eventsRes.status}`);
|
|
173
|
+
}
|
|
174
|
+
const result = await waitForCompletion(sessionId);
|
|
175
|
+
const interactionId = `int_${newId("sesn").slice(5)}`;
|
|
176
|
+
const db = getDb();
|
|
177
|
+
db.prepare(
|
|
178
|
+
`INSERT INTO google_interactions (id, session_id, seq, status, environment_id, created_at) VALUES (?, ?, ?, ?, ?, ?)`
|
|
179
|
+
).run(interactionId, sessionId, 1, result.status, environmentId ?? null, (/* @__PURE__ */ new Date()).toISOString());
|
|
180
|
+
return jsonOk(buildResponse(interactionId, result, environmentId));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function handleGetInteraction(request, id) {
|
|
184
|
+
return routeWrap(request, async () => {
|
|
185
|
+
ensureTable();
|
|
186
|
+
const db = getDb();
|
|
187
|
+
const row = db.prepare(
|
|
188
|
+
`SELECT id, session_id, seq, status, environment_id, created_at FROM google_interactions WHERE id = ?`
|
|
189
|
+
).get(id);
|
|
190
|
+
if (!row) throw notFound(`interaction not found: ${id}`);
|
|
191
|
+
const { listEvents } = await import("./db/events.js");
|
|
192
|
+
const { rowToManagedEvent } = await import("./db/events.js");
|
|
193
|
+
const eventRows = listEvents(row.session_id, { limit: 500, order: "asc" });
|
|
194
|
+
const steps = [];
|
|
195
|
+
let inputTokens = 0;
|
|
196
|
+
let outputTokens = 0;
|
|
197
|
+
for (const eventRow of eventRows) {
|
|
198
|
+
const event = rowToManagedEvent(eventRow);
|
|
199
|
+
if (event.type === "agent.message") {
|
|
200
|
+
const content = event.content;
|
|
201
|
+
const text = content?.filter((c) => c.type === "text").map((c) => c.text).join("") ?? "";
|
|
202
|
+
if (text) {
|
|
203
|
+
steps.push({ type: "model_output", content: [{ type: "text", text }] });
|
|
204
|
+
}
|
|
205
|
+
} else if (event.type === "agent.tool_use") {
|
|
206
|
+
steps.push({
|
|
207
|
+
type: "function_call",
|
|
208
|
+
id: event.tool_use_id ?? "",
|
|
209
|
+
name: event.name ?? "",
|
|
210
|
+
arguments: event.input ?? {}
|
|
211
|
+
});
|
|
212
|
+
} else if (event.type === "agent.tool_result") {
|
|
213
|
+
steps.push({
|
|
214
|
+
type: "code_execution_result",
|
|
215
|
+
call_id: event.tool_use_id ?? "",
|
|
216
|
+
result: JSON.stringify(event.content ?? "")
|
|
217
|
+
});
|
|
218
|
+
} else if (event.type === "agent.custom_tool_use") {
|
|
219
|
+
steps.push({
|
|
220
|
+
type: "function_call",
|
|
221
|
+
id: event.tool_use_id ?? "",
|
|
222
|
+
name: event.name ?? "",
|
|
223
|
+
arguments: event.input ?? {}
|
|
224
|
+
});
|
|
225
|
+
} else if (event.type === "span.model_request_end") {
|
|
226
|
+
const mu = event.model_usage;
|
|
227
|
+
inputTokens += mu?.input_tokens ?? 0;
|
|
228
|
+
outputTokens += mu?.output_tokens ?? 0;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const response = {
|
|
232
|
+
id: row.id,
|
|
233
|
+
created: row.created_at,
|
|
234
|
+
updated: row.created_at,
|
|
235
|
+
status: row.status,
|
|
236
|
+
steps,
|
|
237
|
+
usage: { total_input_tokens: inputTokens, total_output_tokens: outputTokens, total_tokens: inputTokens + outputTokens },
|
|
238
|
+
environment_id: row.environment_id ?? void 0
|
|
239
|
+
};
|
|
240
|
+
return jsonOk(response);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
function handleDeleteInteraction(request, id) {
|
|
244
|
+
return routeWrap(request, async () => {
|
|
245
|
+
ensureTable();
|
|
246
|
+
const db = getDb();
|
|
247
|
+
const row = db.prepare(
|
|
248
|
+
`SELECT id, session_id FROM google_interactions WHERE id = ?`
|
|
249
|
+
).get(id);
|
|
250
|
+
if (!row) throw notFound(`interaction not found: ${id}`);
|
|
251
|
+
db.prepare(`DELETE FROM google_interactions WHERE id = ?`).run(id);
|
|
252
|
+
const { handleDeleteSession } = await import("./handlers/sessions.js");
|
|
253
|
+
const sessReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/sessions/${row.session_id}`), {
|
|
254
|
+
method: "DELETE",
|
|
255
|
+
headers: request.headers
|
|
256
|
+
});
|
|
257
|
+
await handleDeleteSession(sessReq, row.session_id).catch(() => {
|
|
258
|
+
});
|
|
259
|
+
return jsonOk({ id, deleted: true });
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
function handleCancelInteraction(request, id) {
|
|
263
|
+
return routeWrap(request, async () => {
|
|
264
|
+
ensureTable();
|
|
265
|
+
const db = getDb();
|
|
266
|
+
const row = db.prepare(
|
|
267
|
+
`SELECT id, session_id, seq, status, environment_id, created_at FROM google_interactions WHERE id = ?`
|
|
268
|
+
).get(id);
|
|
269
|
+
if (!row) throw notFound(`interaction not found: ${id}`);
|
|
270
|
+
const { handlePostEvents } = await import("./handlers/events.js");
|
|
271
|
+
const eventsReq = new Request(request.url.replace(/\/google\/v1beta\/interactions.*/, `/v1/sessions/${row.session_id}/events`), {
|
|
272
|
+
method: "POST",
|
|
273
|
+
headers: request.headers,
|
|
274
|
+
body: JSON.stringify({
|
|
275
|
+
events: [{ type: "user.interrupt" }]
|
|
276
|
+
})
|
|
277
|
+
});
|
|
278
|
+
await handlePostEvents(eventsReq, row.session_id).catch(() => {
|
|
279
|
+
});
|
|
280
|
+
db.prepare(`UPDATE google_interactions SET status = 'cancelled' WHERE id = ?`).run(id);
|
|
281
|
+
const response = {
|
|
282
|
+
id: row.id,
|
|
283
|
+
created: row.created_at,
|
|
284
|
+
updated: (/* @__PURE__ */ new Date()).toISOString(),
|
|
285
|
+
status: "cancelled",
|
|
286
|
+
steps: [],
|
|
287
|
+
usage: { total_input_tokens: 0, total_output_tokens: 0, total_tokens: 0 },
|
|
288
|
+
environment_id: row.environment_id ?? void 0
|
|
289
|
+
};
|
|
290
|
+
return jsonOk(response);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
function buildFunctionResultEvents(input) {
|
|
294
|
+
if (typeof input === "string") return [];
|
|
295
|
+
if (!Array.isArray(input)) return [];
|
|
296
|
+
const results = [];
|
|
297
|
+
for (const item of input) {
|
|
298
|
+
if (item && typeof item === "object" && item.type === "function_result") {
|
|
299
|
+
const callId = item.call_id ?? item.id ?? "";
|
|
300
|
+
const resultText = item.result ?? item.output ?? "";
|
|
301
|
+
results.push({
|
|
302
|
+
type: "user.custom_tool_result",
|
|
303
|
+
custom_tool_use_id: callId,
|
|
304
|
+
content: [{ type: "text", text: typeof resultText === "string" ? resultText : JSON.stringify(resultText) }]
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return results;
|
|
309
|
+
}
|
|
310
|
+
async function waitForCompletion(sessionId, afterSeq = 0) {
|
|
311
|
+
const { subscribe } = await import("./sessions/bus.js");
|
|
312
|
+
return new Promise((resolve) => {
|
|
313
|
+
const steps = [];
|
|
314
|
+
let outputText = "";
|
|
315
|
+
let inputTokens = 0;
|
|
316
|
+
let outputTokens = 0;
|
|
317
|
+
let status = "completed";
|
|
318
|
+
let resolved = false;
|
|
319
|
+
let subscription = null;
|
|
320
|
+
const timeout = setTimeout(() => {
|
|
321
|
+
if (resolved) return;
|
|
322
|
+
resolved = true;
|
|
323
|
+
subscription?.unsubscribe();
|
|
324
|
+
resolve({
|
|
325
|
+
status: "failed",
|
|
326
|
+
steps,
|
|
327
|
+
usage: { total_input_tokens: inputTokens, total_output_tokens: outputTokens, total_tokens: inputTokens + outputTokens },
|
|
328
|
+
outputText
|
|
329
|
+
});
|
|
330
|
+
}, 5 * 6e4);
|
|
331
|
+
function finish() {
|
|
332
|
+
if (resolved) return;
|
|
333
|
+
resolved = true;
|
|
334
|
+
clearTimeout(timeout);
|
|
335
|
+
subscription?.unsubscribe();
|
|
336
|
+
resolve({
|
|
337
|
+
status,
|
|
338
|
+
steps,
|
|
339
|
+
usage: { total_input_tokens: inputTokens, total_output_tokens: outputTokens, total_tokens: inputTokens + outputTokens },
|
|
340
|
+
outputText
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function handleEvent(event) {
|
|
344
|
+
if (resolved) return;
|
|
345
|
+
if (event.type === "agent.message") {
|
|
346
|
+
const content = event.content;
|
|
347
|
+
const text = content?.filter((c) => c.type === "text").map((c) => c.text).join("") ?? "";
|
|
348
|
+
if (text) {
|
|
349
|
+
outputText += text;
|
|
350
|
+
steps.push({ type: "model_output", content: [{ type: "text", text }] });
|
|
351
|
+
}
|
|
352
|
+
} else if (event.type === "agent.tool_use") {
|
|
353
|
+
steps.push({
|
|
354
|
+
type: "function_call",
|
|
355
|
+
id: event.tool_use_id ?? "",
|
|
356
|
+
name: event.name ?? "",
|
|
357
|
+
arguments: event.input ?? {}
|
|
358
|
+
});
|
|
359
|
+
} else if (event.type === "agent.tool_result") {
|
|
360
|
+
steps.push({
|
|
361
|
+
type: "code_execution_result",
|
|
362
|
+
call_id: event.tool_use_id ?? "",
|
|
363
|
+
result: JSON.stringify(event.content ?? "")
|
|
364
|
+
});
|
|
365
|
+
} else if (event.type === "agent.custom_tool_use") {
|
|
366
|
+
status = "requires_action";
|
|
367
|
+
steps.push({
|
|
368
|
+
type: "function_call",
|
|
369
|
+
id: event.tool_use_id ?? "",
|
|
370
|
+
name: event.name ?? "",
|
|
371
|
+
arguments: event.input ?? {}
|
|
372
|
+
});
|
|
373
|
+
} else if (event.type === "span.model_request_end") {
|
|
374
|
+
const mu = event.model_usage;
|
|
375
|
+
inputTokens += mu?.input_tokens ?? 0;
|
|
376
|
+
outputTokens += mu?.output_tokens ?? 0;
|
|
377
|
+
} else if (event.type === "session.status_idle") {
|
|
378
|
+
const stopReason = event.stop_reason;
|
|
379
|
+
if (stopReason === "error") status = "failed";
|
|
380
|
+
else if (status !== "requires_action") status = "completed";
|
|
381
|
+
finish();
|
|
382
|
+
} else if (event.type === "session.error") {
|
|
383
|
+
status = "failed";
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const sub = subscribe(sessionId, afterSeq, handleEvent);
|
|
387
|
+
subscription = sub;
|
|
388
|
+
if (resolved) {
|
|
389
|
+
clearTimeout(timeout);
|
|
390
|
+
sub.unsubscribe();
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function buildResponse(id, result, environmentId) {
|
|
395
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
396
|
+
return {
|
|
397
|
+
id,
|
|
398
|
+
created: now,
|
|
399
|
+
updated: now,
|
|
400
|
+
status: result.status,
|
|
401
|
+
steps: result.steps,
|
|
402
|
+
usage: result.usage,
|
|
403
|
+
environment_id: environmentId
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export {
|
|
408
|
+
handleCreateInteraction,
|
|
409
|
+
handleGetInteraction,
|
|
410
|
+
handleDeleteInteraction,
|
|
411
|
+
handleCancelInteraction
|
|
412
|
+
};
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
reconcileDockerOrphanSandboxes,
|
|
7
7
|
reconcileOrphanSandboxes,
|
|
8
8
|
releaseSession
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-ZCCHLDLC.js";
|
|
10
10
|
import {
|
|
11
11
|
appendEvent,
|
|
12
12
|
dropEmitter
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
} from "./chunk-ZC7OR65K.js";
|
|
20
20
|
import {
|
|
21
21
|
tryResolveProvider
|
|
22
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-XLMNSDUJ.js";
|
|
23
23
|
import {
|
|
24
24
|
getEnvironment
|
|
25
25
|
} from "./chunk-NSUVDKNC.js";
|