@agimon-ai/po-codemode 0.0.1-alpha.1 → 0.0.1-alpha.100
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 +23 -11
- package/dist/codemodeTools-CB5QuVPw.mjs +826 -0
- package/dist/codemodeTools-CB5QuVPw.mjs.map +1 -0
- package/dist/codemodeTools-IS_qQ7PF.cjs +837 -0
- package/dist/codemodeTools-IS_qQ7PF.cjs.map +1 -0
- package/dist/extensions/native.mjs +449 -0
- package/dist/extensions/native.mjs.map +1 -0
- package/dist/extensions/server.cjs +585 -24
- package/dist/extensions/server.cjs.map +1 -1
- package/dist/extensions/server.d.mts.map +1 -1
- package/dist/extensions/server.mjs +587 -26
- package/dist/extensions/server.mjs.map +1 -1
- package/dist/extensions/web.mjs +460 -0
- package/dist/extensions/web.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +27 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/llms.txt +4 -1
- package/package.json +83 -8
- package/src/prompts/codemode-how-to-llm/SKILL.md +29 -0
- package/src/prompts/codemode-how-to-page/SKILL.md +38 -0
- package/src/prompts/codemode-how-to-shell/SKILL.md +21 -0
- package/src/prompts/how-to-codemode/SKILL.md +72 -0
- package/dist/codemodeTools-BX2qieCR.cjs +0 -349
- package/dist/codemodeTools-BX2qieCR.cjs.map +0 -1
- package/dist/codemodeTools-CSYUxIeA.mjs +0 -338
- package/dist/codemodeTools-CSYUxIeA.mjs.map +0 -1
- package/src/prompts/po-use-codemode/SKILL.md +0 -44
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { n as createCodemodeRegistry, t as createCodemodeTools } from "../codemodeTools-
|
|
2
|
-
import { PO_CODEMODE_CONTRACT
|
|
1
|
+
import { n as createCodemodeRegistry, t as createCodemodeTools } from "../codemodeTools-CB5QuVPw.mjs";
|
|
2
|
+
import { PO_CODEMODE_CONTRACT } from "@agimon-ai/po-core/codemode";
|
|
3
|
+
import { PO_APPROVAL_SERVICE, readPoApproval } from "@agimon-ai/po-core/approval";
|
|
4
|
+
import { readPoHeadlessHost } from "@agimon-ai/po-core/headless";
|
|
5
|
+
import { Type } from "typebox";
|
|
3
6
|
import { PO_SERVER_SCOPE_SERVICE, composeServerProviderOverrides, defineServerPlugin, packageResourcePath, readPackageResource, requirePoServerScope } from "@agimon-ai/po-core/serverPlugin";
|
|
4
7
|
import { composeRootHooks, composeRootInjects, defineResource, defineRoot, defineServerTool, defineServiceContract } from "@agimon-ai/po-core/extensionFile";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
8
|
+
import { PO_CONFIG_SERVICE } from "@agimon-ai/po-config";
|
|
9
|
+
import { PO_BACKGROUND_WORK_SERVICE, readPoBackgroundWorkService } from "@agimon-ai/po-core/backgroundWork";
|
|
10
|
+
import { BACKGROUND_CONTEXT, PO_BROWSER_SERVICE, PO_SESSION_FILES_SERVICE, PO_SHELL_SERVICE, getOrThrow, readPoBrowser, readPoFs, readPoIsolate, readPoNpm, withAbortSignal } from "@agimon-ai/po-core/platform";
|
|
11
|
+
import { readMajorModesConfig, readPackageConfigurations } from "@agimon-ai/po-config/majorModes";
|
|
12
|
+
import { Check } from "typebox/value";
|
|
13
|
+
import { Check as Check$1, Errors, Meta } from "typebox/schema";
|
|
7
14
|
//#region src/services/codemodeScripts/index.ts
|
|
8
15
|
const SESSION_FOLDER = "codemode";
|
|
9
16
|
const SESSION_PREFIX = "session:";
|
|
@@ -92,10 +99,13 @@ var FileSystemCodemodeScriptsService = class {
|
|
|
92
99
|
const fs = this.options.fs();
|
|
93
100
|
if (!fs) return [];
|
|
94
101
|
const found = [];
|
|
95
|
-
for (const source of this.sources(domains))
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
for (const source of this.sources(domains)) {
|
|
103
|
+
const names = await this.names(fs, source.directory).catch(() => []);
|
|
104
|
+
for (const name of names) found.push({
|
|
105
|
+
specifier: `${source.domain}/${name.slice(0, -3)}`,
|
|
106
|
+
path: `${source.directory}/${name}`
|
|
107
|
+
});
|
|
108
|
+
}
|
|
99
109
|
const session = this.sessionDirectory();
|
|
100
110
|
if (session !== void 0) for (const name of (await this.names(fs, session)).reverse()) found.push({
|
|
101
111
|
specifier: `${SESSION_PREFIX}${name.slice(0, -3)}`,
|
|
@@ -113,10 +123,457 @@ var FileSystemCodemodeScriptsService = class {
|
|
|
113
123
|
}
|
|
114
124
|
};
|
|
115
125
|
//#endregion
|
|
126
|
+
//#region src/schemas/packageConfig.ts
|
|
127
|
+
const CODEMODE_PACKAGE = "@agimon-ai/po-codemode";
|
|
128
|
+
const CodemodePackageConfigSchema = Type.Object({ llmModel: Type.Optional(Type.String({
|
|
129
|
+
pattern: "^[^/\\s]+/\\S+$",
|
|
130
|
+
maxLength: 256
|
|
131
|
+
})) }, { additionalProperties: false });
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/services/llmConfig/index.ts
|
|
134
|
+
const PO_CODEMODE_LLM_CONFIG_SERVICE = "po/codemode-llm-config";
|
|
135
|
+
/** Session policy is loaded once per immutable Po config snapshot, including failed loads. */
|
|
136
|
+
var LlmConfigService = class {
|
|
137
|
+
io;
|
|
138
|
+
snapshot;
|
|
139
|
+
loads = /* @__PURE__ */ new WeakMap();
|
|
140
|
+
constructor(io) {
|
|
141
|
+
this.io = io;
|
|
142
|
+
}
|
|
143
|
+
bind(snapshot) {
|
|
144
|
+
this.snapshot = snapshot;
|
|
145
|
+
return () => {
|
|
146
|
+
if (this.snapshot === snapshot) this.snapshot = void 0;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
current() {
|
|
150
|
+
if (!this.snapshot) return Promise.resolve({});
|
|
151
|
+
const snapshot = this.snapshot();
|
|
152
|
+
let load = this.loads.get(snapshot);
|
|
153
|
+
if (!load) {
|
|
154
|
+
load = this.load(snapshot);
|
|
155
|
+
this.loads.set(snapshot, load);
|
|
156
|
+
}
|
|
157
|
+
return load;
|
|
158
|
+
}
|
|
159
|
+
async load(snapshot) {
|
|
160
|
+
if (!snapshot.harness.root) return {};
|
|
161
|
+
const { environment, homeDirectory } = this.io;
|
|
162
|
+
const fs = this.io.fs();
|
|
163
|
+
if (!fs || !homeDirectory) throw new Error("Code mode LLM configuration requires host filesystem and home.");
|
|
164
|
+
const modes = await readMajorModesConfig(fs, snapshot.harness.root, BACKGROUND_CONTEXT, {
|
|
165
|
+
environment,
|
|
166
|
+
homeDirectory
|
|
167
|
+
});
|
|
168
|
+
const configurations = await readPackageConfigurations(fs, modes, [...snapshot.harness.layers], CODEMODE_PACKAGE, BACKGROUND_CONTEXT);
|
|
169
|
+
let merged = {};
|
|
170
|
+
for (const entry of configurations) {
|
|
171
|
+
if (!Check(CodemodePackageConfigSchema, entry.config)) throw new TypeError("Invalid code mode package configuration: llmModel must be provider/modelId.");
|
|
172
|
+
merged = {
|
|
173
|
+
...merged,
|
|
174
|
+
...entry.config
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return merged;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/services/llmNamespace/index.ts
|
|
182
|
+
const MAX_INPUT_BYTES = 65536;
|
|
183
|
+
const MAX_SCHEMA_BYTES = 16384;
|
|
184
|
+
const MAX_OUTPUT_BYTES = 65536;
|
|
185
|
+
const MAX_CALLS = 8;
|
|
186
|
+
const MAX_CONCURRENT = 2;
|
|
187
|
+
const DEFAULT_TOKENS = 1024;
|
|
188
|
+
const MAX_TOKENS = 4096;
|
|
189
|
+
const RESULT_TOOL = "return_result";
|
|
190
|
+
const MODEL = /^[^/\s]+\/\S+$/u;
|
|
191
|
+
const META = Meta["http://json-schema.org/draft-07/schema#"];
|
|
192
|
+
const DECLARATIONS$2 = `interface LlmRequest {
|
|
193
|
+
prompt: string;
|
|
194
|
+
/** Explicit JSON input only, never the surrounding conversation. */
|
|
195
|
+
input?: unknown;
|
|
196
|
+
system?: string;
|
|
197
|
+
/** provider/modelId; overrides llmModel config and the live session model. */
|
|
198
|
+
model?: string;
|
|
199
|
+
/** Defaults to 1024, maximum 4096. */
|
|
200
|
+
maxTokens?: number;
|
|
201
|
+
}
|
|
202
|
+
declare const llm: {
|
|
203
|
+
/** One-shot text transform. At most 8 calls per exec, 2 concurrently. */
|
|
204
|
+
complete(request: LlmRequest): Promise<{ text: string; model: string }>;
|
|
205
|
+
/** Object-root JSON Schema (draft 7), local nonrecursive refs only. Invalid output rejects without retry. */
|
|
206
|
+
json(request: LlmRequest & { schema: Record<string, unknown> }): Promise<{ value: unknown; model: string }>;
|
|
207
|
+
};`;
|
|
208
|
+
function jsonText(value, limit, label) {
|
|
209
|
+
const json = JSON.stringify(value, (_key, item) => {
|
|
210
|
+
if (item === void 0 || typeof item === "function" || typeof item === "symbol" || typeof item === "bigint" || typeof item === "number" && !Number.isFinite(item)) throw new TypeError(`${label} must be JSON serializable.`);
|
|
211
|
+
return item;
|
|
212
|
+
});
|
|
213
|
+
if (json === void 0) throw new TypeError(`${label} must be JSON serializable.`);
|
|
214
|
+
if (new TextEncoder().encode(json).length > limit) throw new RangeError(`${label} exceeds ${limit} bytes.`);
|
|
215
|
+
return json;
|
|
216
|
+
}
|
|
217
|
+
function object(value) {
|
|
218
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
219
|
+
}
|
|
220
|
+
/** Reject unsupported references and recursive schemas before spending tokens or entering the validator. */
|
|
221
|
+
function checkedSchema(value) {
|
|
222
|
+
const schema = JSON.parse(jsonText(value, MAX_SCHEMA_BYTES, "LLM schema"));
|
|
223
|
+
if (!object(schema) || schema.type !== "object") throw new TypeError("LLM schema must have type: object.");
|
|
224
|
+
if (schema.$schema !== void 0 && schema.$schema !== "http://json-schema.org/draft-07/schema#") throw new TypeError("LLM schema supports JSON Schema draft 7 only.");
|
|
225
|
+
if (!Check$1(META, schema)) throw new TypeError("Invalid LLM JSON Schema.");
|
|
226
|
+
let expanded = 0;
|
|
227
|
+
const visit = (node, ancestors) => {
|
|
228
|
+
if (!object(node)) return;
|
|
229
|
+
if (++expanded > 1024) throw new TypeError("LLM schema exceeds 1024 expanded schema nodes.");
|
|
230
|
+
if (ancestors.length > 32 || ancestors.includes(node)) throw new TypeError("LLM schema references must be nonrecursive and at most 32 levels deep.");
|
|
231
|
+
const stack = [...ancestors, node];
|
|
232
|
+
if (node.$dynamicRef !== void 0 || node.$recursiveRef !== void 0 || node.$id !== void 0) throw new TypeError("LLM schema supports local $ref only, without $id.");
|
|
233
|
+
if (node.$ref !== void 0) {
|
|
234
|
+
if (typeof node.$ref !== "string" || !node.$ref.startsWith("#/")) throw new TypeError("LLM schema supports local JSON-pointer $ref only.");
|
|
235
|
+
let resolved = schema;
|
|
236
|
+
for (const segment of node.$ref.slice(2).split("/")) {
|
|
237
|
+
const key = segment.replace(/~1/gu, "/").replace(/~0/gu, "~");
|
|
238
|
+
resolved = resolved !== null && typeof resolved === "object" && Object.hasOwn(resolved, key) ? Reflect.get(resolved, key) : void 0;
|
|
239
|
+
}
|
|
240
|
+
if (!object(resolved) && typeof resolved !== "boolean") throw new TypeError("Unresolved LLM schema $ref.");
|
|
241
|
+
visit(resolved, stack);
|
|
242
|
+
}
|
|
243
|
+
for (const key of [
|
|
244
|
+
"properties",
|
|
245
|
+
"patternProperties",
|
|
246
|
+
"definitions",
|
|
247
|
+
"$defs",
|
|
248
|
+
"dependencies"
|
|
249
|
+
]) if (object(node[key])) for (const child of Object.values(node[key])) visit(child, stack);
|
|
250
|
+
for (const key of [
|
|
251
|
+
"items",
|
|
252
|
+
"additionalItems",
|
|
253
|
+
"additionalProperties",
|
|
254
|
+
"contains",
|
|
255
|
+
"propertyNames",
|
|
256
|
+
"not",
|
|
257
|
+
"if",
|
|
258
|
+
"then",
|
|
259
|
+
"else",
|
|
260
|
+
"allOf",
|
|
261
|
+
"anyOf",
|
|
262
|
+
"oneOf"
|
|
263
|
+
]) {
|
|
264
|
+
const child = node[key];
|
|
265
|
+
if (Array.isArray(child)) for (const item of child) visit(item, stack);
|
|
266
|
+
else visit(child, stack);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
visit(schema, []);
|
|
270
|
+
return schema;
|
|
271
|
+
}
|
|
272
|
+
function request$1(value, structured) {
|
|
273
|
+
if (!object(value)) throw new TypeError("LLM request must be an object.");
|
|
274
|
+
const allowed = [
|
|
275
|
+
"prompt",
|
|
276
|
+
"input",
|
|
277
|
+
"system",
|
|
278
|
+
"model",
|
|
279
|
+
"maxTokens",
|
|
280
|
+
...structured ? ["schema"] : []
|
|
281
|
+
];
|
|
282
|
+
if (Object.keys(value).some((key) => !allowed.includes(key))) throw new TypeError("Unknown LLM request field.");
|
|
283
|
+
if (typeof value.prompt !== "string" || !value.prompt.trim()) throw new TypeError("LLM prompt must be nonempty text.");
|
|
284
|
+
if (value.system !== void 0 && typeof value.system !== "string") throw new TypeError("LLM system must be text.");
|
|
285
|
+
if (value.model !== void 0 && (typeof value.model !== "string" || !MODEL.test(value.model) || value.model.length > 256)) throw new TypeError("LLM model must be provider/modelId.");
|
|
286
|
+
if (value.maxTokens !== void 0 && (!Number.isInteger(value.maxTokens) || Number(value.maxTokens) < 1 || Number(value.maxTokens) > MAX_TOKENS)) throw new RangeError(`LLM maxTokens must be an integer from 1 to ${MAX_TOKENS}.`);
|
|
287
|
+
return {
|
|
288
|
+
prompt: value.prompt,
|
|
289
|
+
...value.input === void 0 ? {} : { input: value.input },
|
|
290
|
+
...value.system === void 0 ? {} : { system: value.system },
|
|
291
|
+
...value.model === void 0 ? {} : { model: value.model },
|
|
292
|
+
...value.maxTokens === void 0 ? {} : { maxTokens: value.maxTokens },
|
|
293
|
+
...structured ? { schema: checkedSchema(value.schema) } : {}
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/** Explicit data transforms over the host's guarded completions, never a tool-using agent. */
|
|
297
|
+
function createLlmNamespace(options) {
|
|
298
|
+
const { context } = options;
|
|
299
|
+
return {
|
|
300
|
+
name: "llm",
|
|
301
|
+
summary: "One-shot LLM text and schema-validated JSON transforms. Explicit input only, bounded calls, no tools or retries.",
|
|
302
|
+
declarations: DECLARATIONS$2,
|
|
303
|
+
portable: true,
|
|
304
|
+
bind(run) {
|
|
305
|
+
if (run.sessionId !== context.sessionId) throw new Error("LLM namespace belongs to another session.");
|
|
306
|
+
let disposed = false;
|
|
307
|
+
let calls = 0;
|
|
308
|
+
let active = 0;
|
|
309
|
+
run.onDispose(() => {
|
|
310
|
+
disposed = true;
|
|
311
|
+
});
|
|
312
|
+
const available = () => {
|
|
313
|
+
run.signal.throwIfAborted();
|
|
314
|
+
if (disposed) throw new Error("LLM namespace run is disposed.");
|
|
315
|
+
};
|
|
316
|
+
const call = async (input, structured) => {
|
|
317
|
+
available();
|
|
318
|
+
if (calls >= MAX_CALLS) throw new RangeError(`At most ${MAX_CALLS} LLM calls per exec.`);
|
|
319
|
+
if (active >= MAX_CONCURRENT) throw new RangeError(`At most ${MAX_CONCURRENT} concurrent LLM calls per exec.`);
|
|
320
|
+
const params = request$1(input, structured);
|
|
321
|
+
const userInput = params.input === void 0 ? params.prompt : `${params.prompt}\n\nInput (JSON):\n${jsonText(params.input, MAX_INPUT_BYTES, "LLM input")}`;
|
|
322
|
+
const systemPrompt = `${params.system ?? "Transform only the explicit input according to the request."}${structured ? "\nCall return_result exactly once with the result. Do not call other tools." : ""}`;
|
|
323
|
+
if (new TextEncoder().encode(systemPrompt + userInput).length > MAX_INPUT_BYTES) throw new RangeError("LLM prompt and input exceed 65536 bytes.");
|
|
324
|
+
active += 1;
|
|
325
|
+
const started = Date.now();
|
|
326
|
+
let model;
|
|
327
|
+
let tokens;
|
|
328
|
+
const record = (ok, error) => run.record?.({
|
|
329
|
+
name: structured ? "llm.json" : "llm.complete",
|
|
330
|
+
tool: "llm",
|
|
331
|
+
args: {
|
|
332
|
+
...model ? { model } : {},
|
|
333
|
+
maxTokens: params.maxTokens ?? DEFAULT_TOKENS
|
|
334
|
+
},
|
|
335
|
+
ok,
|
|
336
|
+
ms: Date.now() - started,
|
|
337
|
+
...error === void 0 ? {} : { error },
|
|
338
|
+
...tokens === void 0 ? {} : { result: {
|
|
339
|
+
content: [],
|
|
340
|
+
details: { totalTokens: tokens }
|
|
341
|
+
} }
|
|
342
|
+
});
|
|
343
|
+
try {
|
|
344
|
+
const config = await options.config();
|
|
345
|
+
const current = params.model || config.llmModel ? void 0 : (await context.session.readModelSettings?.())?.model ?? context.model;
|
|
346
|
+
model = params.model ?? config.llmModel ?? (current ? `${current.provider}/${current.id}` : void 0);
|
|
347
|
+
available();
|
|
348
|
+
if (!model || !context.textCompletion?.available(model)) throw new Error(`LLM model is unavailable${model ? `: ${model}` : ""}.`);
|
|
349
|
+
if (structured ? !context.toolCompletion : !context.textCompletion) throw new Error("LLM completion is unavailable on this host.");
|
|
350
|
+
if (calls >= MAX_CALLS) throw new RangeError(`At most ${MAX_CALLS} LLM calls per exec.`);
|
|
351
|
+
calls += 1;
|
|
352
|
+
const completion = {
|
|
353
|
+
systemPrompt,
|
|
354
|
+
input: userInput,
|
|
355
|
+
maxTokens: params.maxTokens ?? DEFAULT_TOKENS,
|
|
356
|
+
signal: run.signal,
|
|
357
|
+
recordUsage: true
|
|
358
|
+
};
|
|
359
|
+
if (!structured) {
|
|
360
|
+
const text = await context.textCompletion.complete(model, completion);
|
|
361
|
+
available();
|
|
362
|
+
jsonText(text, MAX_OUTPUT_BYTES, "LLM output");
|
|
363
|
+
record(true);
|
|
364
|
+
return {
|
|
365
|
+
text,
|
|
366
|
+
model
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
const schema = params.schema;
|
|
370
|
+
const response = await context.toolCompletion.complete(model, {
|
|
371
|
+
...completion,
|
|
372
|
+
tools: [{
|
|
373
|
+
name: RESULT_TOOL,
|
|
374
|
+
description: "Return the requested structured result.",
|
|
375
|
+
parameters: schema
|
|
376
|
+
}]
|
|
377
|
+
});
|
|
378
|
+
tokens = response.usage.totalTokens;
|
|
379
|
+
available();
|
|
380
|
+
if (response.toolCalls.length !== 1 || response.toolCalls[0]?.name !== RESULT_TOOL) throw new Error("LLM must return exactly one return_result call.");
|
|
381
|
+
const value = JSON.parse(jsonText(response.toolCalls[0].arguments, MAX_OUTPUT_BYTES, "LLM output"));
|
|
382
|
+
if (!Check$1(schema, value)) {
|
|
383
|
+
const [, errors] = Errors(schema, value);
|
|
384
|
+
throw new TypeError(`LLM result failed schema validation: ${errors[0]?.message ?? "invalid result"}`);
|
|
385
|
+
}
|
|
386
|
+
record(true);
|
|
387
|
+
return {
|
|
388
|
+
value,
|
|
389
|
+
model
|
|
390
|
+
};
|
|
391
|
+
} catch (error) {
|
|
392
|
+
record(false, error instanceof Error ? error.message : String(error));
|
|
393
|
+
throw error;
|
|
394
|
+
} finally {
|
|
395
|
+
active -= 1;
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
return {
|
|
399
|
+
complete: (input) => call(input, false),
|
|
400
|
+
json: (input) => call(input, true)
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/services/pageNamespace/index.ts
|
|
407
|
+
const MAX_URL_CHARS = 2048;
|
|
408
|
+
const DECLARATIONS$1 = `interface RenderedPage {
|
|
409
|
+
/** Final URL after redirects the host allowed. */
|
|
410
|
+
url: string;
|
|
411
|
+
status?: number;
|
|
412
|
+
title: string;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
declare const page: {
|
|
416
|
+
/** Renders the page in a remote browser and returns its visible text, capped. */
|
|
417
|
+
fetchPage(url: string): Promise<RenderedPage & { text: string; truncated: boolean }>;
|
|
418
|
+
/** The rendered DOM as HTML plus its links, capped. */
|
|
419
|
+
snapshot(url: string): Promise<RenderedPage & { html: string; links: { text: string; href: string }[]; truncated: boolean }>;
|
|
420
|
+
/** Shows a PNG of the page to you in this exec result. Resolves to page info, not image data. */
|
|
421
|
+
screenshot(input: string | { url: string; fullPage?: boolean }): Promise<RenderedPage & { shown: boolean; warning?: string }>;
|
|
422
|
+
};`;
|
|
423
|
+
function target(method, input) {
|
|
424
|
+
const value = typeof input === "string" ? { url: input } : input;
|
|
425
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError(`page.${method} expects a URL string or { url }.`);
|
|
426
|
+
const { url, fullPage } = value;
|
|
427
|
+
if (typeof url !== "string" || !url.trim() || url.length > MAX_URL_CHARS) throw new TypeError(`page.${method} url must be nonempty text of at most ${MAX_URL_CHARS} characters.`);
|
|
428
|
+
if (fullPage !== void 0 && typeof fullPage !== "boolean") throw new TypeError(`page.${method} fullPage must be a boolean.`);
|
|
429
|
+
return {
|
|
430
|
+
url,
|
|
431
|
+
...fullPage === void 0 ? {} : { fullPage }
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/** Rendered pages from the host's remote browser (`po/browser`). Portable: JSON in, JSON out. */
|
|
435
|
+
function createPageNamespace(options) {
|
|
436
|
+
return {
|
|
437
|
+
name: "page",
|
|
438
|
+
summary: "Render public web pages in the host's remote browser: page.fetchPage for text, page.snapshot for HTML and links, page.screenshot to see the page.",
|
|
439
|
+
declarations: DECLARATIONS$1,
|
|
440
|
+
portable: true,
|
|
441
|
+
bind(run) {
|
|
442
|
+
if (run.sessionId !== options.context.sessionId) throw new Error("Page namespace belongs to another session.");
|
|
443
|
+
const disposal = new AbortController();
|
|
444
|
+
const lifetime = AbortSignal.any([run.signal, disposal.signal]);
|
|
445
|
+
run.onDispose(() => disposal.abort(/* @__PURE__ */ new Error("Page namespace run is disposed.")));
|
|
446
|
+
const ready = () => lifetime.throwIfAborted();
|
|
447
|
+
return {
|
|
448
|
+
fetchPage: async (input) => {
|
|
449
|
+
ready();
|
|
450
|
+
return options.browser.fetchPage(target("fetchPage", input).url, lifetime);
|
|
451
|
+
},
|
|
452
|
+
snapshot: async (input) => {
|
|
453
|
+
ready();
|
|
454
|
+
return options.browser.snapshot(target("snapshot", input).url, lifetime);
|
|
455
|
+
},
|
|
456
|
+
screenshot: async (input) => {
|
|
457
|
+
ready();
|
|
458
|
+
const request = target("screenshot", input);
|
|
459
|
+
if (!run.emitImage) throw new Error("page.screenshot needs a host that shows images; use page.fetchPage or page.snapshot.");
|
|
460
|
+
const { data, mimeType, ...page } = await options.browser.screenshot(request, lifetime);
|
|
461
|
+
ready();
|
|
462
|
+
const shown = run.emitImage({
|
|
463
|
+
data,
|
|
464
|
+
mimeType
|
|
465
|
+
});
|
|
466
|
+
return shown.emitted ? {
|
|
467
|
+
...page,
|
|
468
|
+
shown: true
|
|
469
|
+
} : {
|
|
470
|
+
...page,
|
|
471
|
+
shown: false,
|
|
472
|
+
warning: shown.warning
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
//#endregion
|
|
480
|
+
//#region src/services/shellNamespace/index.ts
|
|
481
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
482
|
+
const MAX_TIMEOUT_MS = 12e4;
|
|
483
|
+
const MAX_COMMAND_CHARS = 65536;
|
|
484
|
+
const MAX_OUTPUT_CHARS = 32768;
|
|
485
|
+
const TRUNCATED = "\n[truncated]";
|
|
486
|
+
const DECLARATIONS = `declare const shell: {
|
|
487
|
+
/** Short foreground Bash command in the session cwd/environment. Nonzero exits are returned; cancellation and timeout reject. */
|
|
488
|
+
bash(input: { command: string; timeoutMs?: number }): Promise<{ stdout: string; stderr: string; exitCode: number }>;
|
|
489
|
+
};`;
|
|
490
|
+
function request(input) {
|
|
491
|
+
if (typeof input === "string") input = { command: input };
|
|
492
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) throw new TypeError("shell.bash expects { command, timeoutMs? } or a command string.");
|
|
493
|
+
const value = input;
|
|
494
|
+
if (Object.keys(value).some((key) => key !== "command" && key !== "timeoutMs")) throw new TypeError("shell.bash accepts only command and timeoutMs.");
|
|
495
|
+
if (typeof value.command !== "string" || !value.command.trim() || value.command.length > MAX_COMMAND_CHARS) throw new TypeError(`shell.bash command must be nonempty text of at most ${MAX_COMMAND_CHARS} characters.`);
|
|
496
|
+
if (value.timeoutMs !== void 0 && (!Number.isInteger(value.timeoutMs) || Number(value.timeoutMs) < 1 || Number(value.timeoutMs) > MAX_TIMEOUT_MS)) throw new RangeError(`shell.bash timeoutMs must be an integer from 1 to ${MAX_TIMEOUT_MS}.`);
|
|
497
|
+
return {
|
|
498
|
+
command: value.command,
|
|
499
|
+
timeoutMs: value.timeoutMs
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
/** Deadline-bound commands for post-processing, separate from Runner's supervised processes. */
|
|
503
|
+
function createShellNamespace(options) {
|
|
504
|
+
return {
|
|
505
|
+
name: "shell",
|
|
506
|
+
summary: "Run short foreground Bash commands with bounded stdout/stderr and a deadline. No background or interactive mode.",
|
|
507
|
+
declarations: DECLARATIONS,
|
|
508
|
+
portable: true,
|
|
509
|
+
bind(run) {
|
|
510
|
+
if (run.sessionId !== options.context.sessionId) throw new Error("Shell namespace belongs to another session.");
|
|
511
|
+
const disposal = new AbortController();
|
|
512
|
+
const lifetime = AbortSignal.any([run.signal, disposal.signal]);
|
|
513
|
+
run.onDispose(() => disposal.abort(/* @__PURE__ */ new Error("Shell namespace run is disposed.")));
|
|
514
|
+
const bash = async (input) => {
|
|
515
|
+
lifetime.throwIfAborted();
|
|
516
|
+
const params = request(input);
|
|
517
|
+
const shell = options.shell();
|
|
518
|
+
if (!shell) throw new Error("Shell commands are not available on this host.");
|
|
519
|
+
const timeoutMs = params.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
520
|
+
const signal = AbortSignal.any([lifetime, AbortSignal.timeout(timeoutMs)]);
|
|
521
|
+
const output = {
|
|
522
|
+
stdout: "",
|
|
523
|
+
stderr: ""
|
|
524
|
+
};
|
|
525
|
+
const truncated = {
|
|
526
|
+
stdout: false,
|
|
527
|
+
stderr: false
|
|
528
|
+
};
|
|
529
|
+
const started = Date.now();
|
|
530
|
+
let ok = false;
|
|
531
|
+
let failure;
|
|
532
|
+
try {
|
|
533
|
+
const result = await shell.exec(params.command, {
|
|
534
|
+
cwd: options.context.cwd,
|
|
535
|
+
env: Object.fromEntries(Object.entries(options.context.environment).filter((entry) => entry[1] !== void 0)),
|
|
536
|
+
inheritEnv: false,
|
|
537
|
+
onOutput: (text, _context, { stream }) => {
|
|
538
|
+
const remaining = MAX_OUTPUT_CHARS - output[stream].length;
|
|
539
|
+
output[stream] += text.slice(0, remaining);
|
|
540
|
+
if (text.length > remaining) truncated[stream] = true;
|
|
541
|
+
}
|
|
542
|
+
}, withAbortSignal(signal, BACKGROUND_CONTEXT));
|
|
543
|
+
signal.throwIfAborted();
|
|
544
|
+
const { exitCode } = getOrThrow(result);
|
|
545
|
+
ok = exitCode === 0;
|
|
546
|
+
return {
|
|
547
|
+
stdout: output.stdout + (truncated.stdout ? TRUNCATED : ""),
|
|
548
|
+
stderr: output.stderr + (truncated.stderr ? TRUNCATED : ""),
|
|
549
|
+
exitCode
|
|
550
|
+
};
|
|
551
|
+
} catch (error) {
|
|
552
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
553
|
+
throw error;
|
|
554
|
+
} finally {
|
|
555
|
+
run.record?.({
|
|
556
|
+
name: "shell.bash",
|
|
557
|
+
tool: "shell",
|
|
558
|
+
args: { timeoutMs },
|
|
559
|
+
ok,
|
|
560
|
+
ms: Date.now() - started,
|
|
561
|
+
...failure === void 0 ? {} : { error: failure }
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
return {
|
|
566
|
+
bash,
|
|
567
|
+
run: bash
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
//#endregion
|
|
116
573
|
//#region src/extensions/workspaces/sessions/(backend)/_lib/root.server.ts
|
|
117
574
|
/** One registry per session: contributors register into it, `discover` and `exec` read it. */
|
|
118
|
-
function createCodemodeSession({ context }) {
|
|
119
|
-
const registry = createCodemodeRegistry();
|
|
575
|
+
function createCodemodeSession({ context, agent, host, signal }) {
|
|
576
|
+
const registry = createCodemodeRegistry({ isActive: (source) => readPoHeadlessHost(context)?.isSourceActive(source) ?? true });
|
|
120
577
|
context.inject([PO_SERVER_SCOPE_SERVICE], (scoped) => requirePoServerScope(scoped).provide(PO_CODEMODE_CONTRACT, registry));
|
|
121
578
|
let temporaryDirectory;
|
|
122
579
|
context.inject([PO_SESSION_FILES_SERVICE], (scoped) => {
|
|
@@ -130,14 +587,72 @@ function createCodemodeSession({ context }) {
|
|
|
130
587
|
fs: () => readPoFs(context),
|
|
131
588
|
temporaryDirectory: () => temporaryDirectory
|
|
132
589
|
});
|
|
133
|
-
|
|
590
|
+
const execution = agent?.context;
|
|
591
|
+
const config = execution ? new LlmConfigService({
|
|
592
|
+
fs: () => readPoFs(context),
|
|
593
|
+
environment: execution.environment,
|
|
594
|
+
homeDirectory: host.context.homeDirectory
|
|
595
|
+
}) : void 0;
|
|
596
|
+
let disposeNamespaces;
|
|
597
|
+
if (execution && config) {
|
|
598
|
+
context.inject([PO_CONFIG_SERVICE], (scoped) => {
|
|
599
|
+
const service = scoped.get(PO_CONFIG_SERVICE);
|
|
600
|
+
return service ? config.bind(() => service.getSnapshot()) : void 0;
|
|
601
|
+
});
|
|
602
|
+
const registration = registry.register("@agimon-ai/po-codemode", createLlmNamespace({
|
|
603
|
+
context: execution,
|
|
604
|
+
config: () => config.current()
|
|
605
|
+
}));
|
|
606
|
+
const shellRegistration = registry.register("@agimon-ai/po-codemode", createShellNamespace({
|
|
607
|
+
context: execution,
|
|
608
|
+
shell: () => context.get(PO_SHELL_SERVICE)
|
|
609
|
+
}));
|
|
610
|
+
disposeNamespaces = () => {
|
|
611
|
+
registration.dispose();
|
|
612
|
+
shellRegistration.dispose();
|
|
613
|
+
};
|
|
614
|
+
context.inject([PO_BROWSER_SERVICE], (scoped) => {
|
|
615
|
+
const browser = readPoBrowser(scoped);
|
|
616
|
+
if (!browser) return void 0;
|
|
617
|
+
const pageRegistration = registry.register("@agimon-ai/po-codemode", createPageNamespace({
|
|
618
|
+
context: execution,
|
|
619
|
+
browser
|
|
620
|
+
}));
|
|
621
|
+
return () => pageRegistration.dispose();
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
let background;
|
|
625
|
+
const tools = createCodemodeTools({
|
|
134
626
|
registry,
|
|
135
|
-
|
|
627
|
+
isolate: () => readPoIsolate(context),
|
|
628
|
+
npm: () => readPoNpm(context),
|
|
629
|
+
scripts,
|
|
630
|
+
signal,
|
|
631
|
+
onBackgroundChange: () => background?.update(),
|
|
632
|
+
approval: () => readPoApproval(context)
|
|
633
|
+
});
|
|
634
|
+
if (execution) context.inject([PO_APPROVAL_SERVICE], (scoped) => readPoApproval(scoped)?.registerExecutor("codemode", (call, signal) => tools.runApproved(call, signal, execution.sessionId)));
|
|
635
|
+
context.inject([PO_BACKGROUND_WORK_SERVICE], (scoped) => {
|
|
636
|
+
const registration = readPoBackgroundWorkService(scoped)?.register(tools.backgroundWork);
|
|
637
|
+
background = registration;
|
|
638
|
+
return () => {
|
|
639
|
+
if (background === registration) background = void 0;
|
|
640
|
+
registration?.dispose();
|
|
641
|
+
};
|
|
642
|
+
});
|
|
643
|
+
return {
|
|
644
|
+
value: {
|
|
136
645
|
registry,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
646
|
+
...tools
|
|
647
|
+
},
|
|
648
|
+
services: [(scoped) => {
|
|
649
|
+
if (config) scoped.provide(PO_CODEMODE_LLM_CONFIG_SERVICE, config);
|
|
650
|
+
}],
|
|
651
|
+
onDispose: async () => {
|
|
652
|
+
await tools.dispose();
|
|
653
|
+
disposeNamespaces?.();
|
|
654
|
+
}
|
|
655
|
+
};
|
|
141
656
|
}
|
|
142
657
|
const root = defineRoot(createCodemodeSession);
|
|
143
658
|
//#endregion
|
|
@@ -145,21 +660,57 @@ const root = defineRoot(createCodemodeSession);
|
|
|
145
660
|
var root_server_default = defineRoot(root);
|
|
146
661
|
//#endregion
|
|
147
662
|
//#region src/extensions/workspaces/sessions/(backend)/contract/codemode.server.ts
|
|
148
|
-
var codemode_server_default = defineServiceContract(
|
|
663
|
+
var codemode_server_default = defineServiceContract(PO_CODEMODE_CONTRACT.name, PO_CODEMODE_CONTRACT);
|
|
664
|
+
//#endregion
|
|
665
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-llm.server.ts
|
|
666
|
+
var codemode_how_to_llm_server_default = defineResource({
|
|
667
|
+
name: "codemode-how-to-llm",
|
|
668
|
+
description: "Have a model classify, extract or summarize data inside a script, with JSON output checked against a schema. Use when a script step needs judgement over many items or messy text; answer directly when no script needs the result.",
|
|
669
|
+
pinned: true,
|
|
670
|
+
kind: "skill",
|
|
671
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-llm/SKILL.md"),
|
|
672
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-llm/SKILL.md")
|
|
673
|
+
});
|
|
149
674
|
//#endregion
|
|
150
|
-
//#region src/extensions/workspaces/sessions/(backend)/resource/
|
|
151
|
-
var
|
|
152
|
-
name: "
|
|
153
|
-
description: "Use
|
|
154
|
-
|
|
675
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-page.server.ts
|
|
676
|
+
var codemode_how_to_page_server_default = defineResource({
|
|
677
|
+
name: "codemode-how-to-page",
|
|
678
|
+
description: "Read or look at a public web page rendered in a remote browser. Use when a cloud session needs page text, links or a screenshot and the page namespace is listed.",
|
|
679
|
+
pinned: true,
|
|
155
680
|
kind: "skill",
|
|
156
|
-
path: packageResourcePath(import.meta.url, "src/prompts/
|
|
157
|
-
read: () => readPackageResource(import.meta.url, "src/prompts/
|
|
681
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-page/SKILL.md"),
|
|
682
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-page/SKILL.md")
|
|
683
|
+
});
|
|
684
|
+
//#endregion
|
|
685
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-shell.server.ts
|
|
686
|
+
var codemode_how_to_shell_server_default = defineResource({
|
|
687
|
+
name: "codemode-how-to-shell",
|
|
688
|
+
description: "Run a quick command inside a script and work with its output as data. Use when command output needs parsing or filtering in code; use the bash tool for long-running, interactive or visible commands.",
|
|
689
|
+
pinned: true,
|
|
690
|
+
kind: "skill",
|
|
691
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-shell/SKILL.md"),
|
|
692
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-shell/SKILL.md")
|
|
693
|
+
});
|
|
694
|
+
//#endregion
|
|
695
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/how-to-codemode.server.ts
|
|
696
|
+
var how_to_codemode_server_default = defineResource({
|
|
697
|
+
name: "how-to-codemode",
|
|
698
|
+
description: "Do multi-step work in one call: script loops, filtering and chained calls across live namespaces in exec instead of many separate tool calls. Use when a task repeats a tool, combines namespaces, or needs a long-running workflow.",
|
|
699
|
+
pinned: true,
|
|
700
|
+
kind: "skill",
|
|
701
|
+
path: packageResourcePath(import.meta.url, "src/prompts/how-to-codemode/SKILL.md"),
|
|
702
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/how-to-codemode/SKILL.md")
|
|
158
703
|
});
|
|
159
704
|
//#endregion
|
|
160
705
|
//#region src/extensions/workspaces/sessions/(backend)/tool/discover.server.ts
|
|
161
706
|
var discover_server_default = defineServerTool((context) => context.root.discover);
|
|
162
707
|
//#endregion
|
|
708
|
+
//#region src/extensions/workspaces/sessions/(backend)/tool/exec_background.server.ts
|
|
709
|
+
var exec_background_server_default = defineServerTool((context) => context.root.execBackground);
|
|
710
|
+
//#endregion
|
|
711
|
+
//#region src/extensions/workspaces/sessions/(backend)/tool/exec_cancel.server.ts
|
|
712
|
+
var exec_cancel_server_default = defineServerTool((context) => context.root.execCancel);
|
|
713
|
+
//#endregion
|
|
163
714
|
//#region src/extensions/workspaces/sessions/(backend)/tool/exec.server.ts
|
|
164
715
|
var exec_server_default = defineServerTool((context) => context.root.exec);
|
|
165
716
|
//#endregion
|
|
@@ -183,10 +734,20 @@ const serverPlugin = defineServerPlugin({
|
|
|
183
734
|
return {
|
|
184
735
|
...composeRootHooks(scopeSession),
|
|
185
736
|
get resources() {
|
|
186
|
-
return [
|
|
737
|
+
return [
|
|
738
|
+
at(codemode_how_to_llm_server_default, contextSession),
|
|
739
|
+
at(codemode_how_to_page_server_default, contextSession),
|
|
740
|
+
at(codemode_how_to_shell_server_default, contextSession),
|
|
741
|
+
at(how_to_codemode_server_default, contextSession)
|
|
742
|
+
];
|
|
187
743
|
},
|
|
188
744
|
get tools() {
|
|
189
|
-
return [
|
|
745
|
+
return [
|
|
746
|
+
via({ name: "discover" }, at(discover_server_default, contextSession)),
|
|
747
|
+
via({ name: "exec_background" }, at(exec_background_server_default, contextSession)),
|
|
748
|
+
via({ name: "exec_cancel" }, at(exec_cancel_server_default, contextSession)),
|
|
749
|
+
via({ name: "exec" }, at(exec_server_default, contextSession))
|
|
750
|
+
];
|
|
190
751
|
},
|
|
191
752
|
services: [...scopeSession.services ?? []],
|
|
192
753
|
get activities() {
|