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