@agimon-ai/po-codemode 0.0.1-alpha.2 → 0.0.1-alpha.21
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-De6GNjzR.mjs +761 -0
- package/dist/codemodeTools-De6GNjzR.mjs.map +1 -0
- package/dist/codemodeTools-DiqyGxSU.cjs +772 -0
- package/dist/codemodeTools-DiqyGxSU.cjs.map +1 -0
- package/dist/extensions/native.mjs +419 -0
- package/dist/extensions/native.mjs.map +1 -0
- package/dist/extensions/server.cjs +484 -23
- package/dist/extensions/server.cjs.map +1 -1
- package/dist/extensions/server.d.mts.map +1 -1
- package/dist/extensions/server.mjs +485 -24
- package/dist/extensions/server.mjs.map +1 -1
- package/dist/extensions/web.mjs +214 -117
- package/dist/extensions/web.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +24 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/llms.txt +3 -1
- package/package.json +39 -12
- package/src/prompts/codemode-how-to-llm/SKILL.md +29 -0
- package/src/prompts/codemode-how-to-shell/SKILL.md +21 -0
- package/src/prompts/how-to-codemode/SKILL.md +71 -0
- package/dist/codemodeTools-BdwOARH8.mjs +0 -383
- package/dist/codemodeTools-BdwOARH8.mjs.map +0 -1
- package/dist/codemodeTools-CLgSDqfU.cjs +0 -394
- package/dist/codemodeTools-CLgSDqfU.cjs.map +0 -1
- package/src/prompts/po-use-codemode/SKILL.md +0 -46
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { n as createCodemodeRegistry, t as createCodemodeTools } from "../codemodeTools-
|
|
1
|
+
import { n as createCodemodeRegistry, t as createCodemodeTools } from "../codemodeTools-De6GNjzR.mjs";
|
|
2
2
|
import { PO_CODEMODE_CONTRACT, PO_CODEMODE_SERVICE } from "@agimon-ai/po-core/codemode";
|
|
3
|
+
import { 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_SESSION_FILES_SERVICE, PO_SHELL_SERVICE, getOrThrow, 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,378 @@ 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$1 = `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$1,
|
|
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/shellNamespace/index.ts
|
|
407
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
408
|
+
const MAX_TIMEOUT_MS = 12e4;
|
|
409
|
+
const MAX_COMMAND_CHARS = 65536;
|
|
410
|
+
const MAX_OUTPUT_CHARS = 32768;
|
|
411
|
+
const TRUNCATED = "\n[truncated]";
|
|
412
|
+
const DECLARATIONS = `declare const shell: {
|
|
413
|
+
/** Short foreground Bash command in the session cwd/environment. Nonzero exits are returned; cancellation and timeout reject. */
|
|
414
|
+
bash(input: { command: string; timeoutMs?: number }): Promise<{ stdout: string; stderr: string; exitCode: number }>;
|
|
415
|
+
};`;
|
|
416
|
+
function request(input) {
|
|
417
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) throw new TypeError("shell.bash expects an object.");
|
|
418
|
+
const value = input;
|
|
419
|
+
if (Object.keys(value).some((key) => key !== "command" && key !== "timeoutMs")) throw new TypeError("shell.bash accepts only command and timeoutMs.");
|
|
420
|
+
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.`);
|
|
421
|
+
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}.`);
|
|
422
|
+
return {
|
|
423
|
+
command: value.command,
|
|
424
|
+
timeoutMs: value.timeoutMs
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/** Deadline-bound commands for post-processing, separate from Runner's supervised processes. */
|
|
428
|
+
function createShellNamespace(options) {
|
|
429
|
+
return {
|
|
430
|
+
name: "shell",
|
|
431
|
+
summary: "Run short foreground Bash commands with bounded stdout/stderr and a deadline. No background or interactive mode.",
|
|
432
|
+
declarations: DECLARATIONS,
|
|
433
|
+
portable: true,
|
|
434
|
+
bind(run) {
|
|
435
|
+
if (run.sessionId !== options.context.sessionId) throw new Error("Shell namespace belongs to another session.");
|
|
436
|
+
const disposal = new AbortController();
|
|
437
|
+
const lifetime = AbortSignal.any([run.signal, disposal.signal]);
|
|
438
|
+
run.onDispose(() => disposal.abort(/* @__PURE__ */ new Error("Shell namespace run is disposed.")));
|
|
439
|
+
return { async bash(input) {
|
|
440
|
+
lifetime.throwIfAborted();
|
|
441
|
+
const params = request(input);
|
|
442
|
+
const shell = options.shell();
|
|
443
|
+
if (!shell) throw new Error("Shell commands are not available on this host.");
|
|
444
|
+
const timeoutMs = params.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
445
|
+
const signal = AbortSignal.any([lifetime, AbortSignal.timeout(timeoutMs)]);
|
|
446
|
+
const output = {
|
|
447
|
+
stdout: "",
|
|
448
|
+
stderr: ""
|
|
449
|
+
};
|
|
450
|
+
const truncated = {
|
|
451
|
+
stdout: false,
|
|
452
|
+
stderr: false
|
|
453
|
+
};
|
|
454
|
+
const started = Date.now();
|
|
455
|
+
let ok = false;
|
|
456
|
+
let failure;
|
|
457
|
+
try {
|
|
458
|
+
const result = await shell.exec(params.command, {
|
|
459
|
+
cwd: options.context.cwd,
|
|
460
|
+
env: Object.fromEntries(Object.entries(options.context.environment).filter((entry) => entry[1] !== void 0)),
|
|
461
|
+
inheritEnv: false,
|
|
462
|
+
onOutput: (text, _context, { stream }) => {
|
|
463
|
+
const remaining = MAX_OUTPUT_CHARS - output[stream].length;
|
|
464
|
+
output[stream] += text.slice(0, remaining);
|
|
465
|
+
if (text.length > remaining) truncated[stream] = true;
|
|
466
|
+
}
|
|
467
|
+
}, withAbortSignal(signal, BACKGROUND_CONTEXT));
|
|
468
|
+
signal.throwIfAborted();
|
|
469
|
+
const { exitCode } = getOrThrow(result);
|
|
470
|
+
ok = exitCode === 0;
|
|
471
|
+
return {
|
|
472
|
+
stdout: output.stdout + (truncated.stdout ? TRUNCATED : ""),
|
|
473
|
+
stderr: output.stderr + (truncated.stderr ? TRUNCATED : ""),
|
|
474
|
+
exitCode
|
|
475
|
+
};
|
|
476
|
+
} catch (error) {
|
|
477
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
478
|
+
throw error;
|
|
479
|
+
} finally {
|
|
480
|
+
run.record?.({
|
|
481
|
+
name: "shell.bash",
|
|
482
|
+
tool: "shell",
|
|
483
|
+
args: { timeoutMs },
|
|
484
|
+
ok,
|
|
485
|
+
ms: Date.now() - started,
|
|
486
|
+
...failure === void 0 ? {} : { error: failure }
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
} };
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
//#endregion
|
|
116
494
|
//#region src/extensions/workspaces/sessions/(backend)/_lib/root.server.ts
|
|
117
495
|
/** One registry per session: contributors register into it, `discover` and `exec` read it. */
|
|
118
|
-
function createCodemodeSession({ context }) {
|
|
119
|
-
const registry = createCodemodeRegistry();
|
|
496
|
+
function createCodemodeSession({ context, agent, host, signal }) {
|
|
497
|
+
const registry = createCodemodeRegistry({ isActive: (source) => readPoHeadlessHost(context)?.isSourceActive(source) ?? true });
|
|
120
498
|
context.inject([PO_SERVER_SCOPE_SERVICE], (scoped) => requirePoServerScope(scoped).provide(PO_CODEMODE_CONTRACT, registry));
|
|
121
499
|
let temporaryDirectory;
|
|
122
500
|
context.inject([PO_SESSION_FILES_SERVICE], (scoped) => {
|
|
@@ -130,14 +508,62 @@ function createCodemodeSession({ context }) {
|
|
|
130
508
|
fs: () => readPoFs(context),
|
|
131
509
|
temporaryDirectory: () => temporaryDirectory
|
|
132
510
|
});
|
|
133
|
-
|
|
511
|
+
const execution = agent?.context;
|
|
512
|
+
const config = execution ? new LlmConfigService({
|
|
513
|
+
fs: () => readPoFs(context),
|
|
514
|
+
environment: execution.environment,
|
|
515
|
+
homeDirectory: host.context.homeDirectory
|
|
516
|
+
}) : void 0;
|
|
517
|
+
let disposeNamespaces;
|
|
518
|
+
if (execution && config) {
|
|
519
|
+
context.inject([PO_CONFIG_SERVICE], (scoped) => {
|
|
520
|
+
const service = scoped.get(PO_CONFIG_SERVICE);
|
|
521
|
+
return service ? config.bind(() => service.getSnapshot()) : void 0;
|
|
522
|
+
});
|
|
523
|
+
const registration = registry.register("@agimon-ai/po-codemode", createLlmNamespace({
|
|
524
|
+
context: execution,
|
|
525
|
+
config: () => config.current()
|
|
526
|
+
}));
|
|
527
|
+
const shellRegistration = registry.register("@agimon-ai/po-codemode", createShellNamespace({
|
|
528
|
+
context: execution,
|
|
529
|
+
shell: () => context.get(PO_SHELL_SERVICE)
|
|
530
|
+
}));
|
|
531
|
+
disposeNamespaces = () => {
|
|
532
|
+
registration.dispose();
|
|
533
|
+
shellRegistration.dispose();
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
let background;
|
|
537
|
+
const tools = createCodemodeTools({
|
|
134
538
|
registry,
|
|
135
|
-
|
|
539
|
+
isolate: () => readPoIsolate(context),
|
|
540
|
+
npm: () => readPoNpm(context),
|
|
541
|
+
scripts,
|
|
542
|
+
signal,
|
|
543
|
+
onBackgroundChange: () => background?.update(),
|
|
544
|
+
approval: () => readPoApproval(context)
|
|
545
|
+
});
|
|
546
|
+
context.inject([PO_BACKGROUND_WORK_SERVICE], (scoped) => {
|
|
547
|
+
const registration = readPoBackgroundWorkService(scoped)?.register(tools.backgroundWork);
|
|
548
|
+
background = registration;
|
|
549
|
+
return () => {
|
|
550
|
+
if (background === registration) background = void 0;
|
|
551
|
+
registration?.dispose();
|
|
552
|
+
};
|
|
553
|
+
});
|
|
554
|
+
return {
|
|
555
|
+
value: {
|
|
136
556
|
registry,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
557
|
+
...tools
|
|
558
|
+
},
|
|
559
|
+
services: [(scoped) => {
|
|
560
|
+
if (config) scoped.provide(PO_CODEMODE_LLM_CONFIG_SERVICE, config);
|
|
561
|
+
}],
|
|
562
|
+
onDispose: async () => {
|
|
563
|
+
await tools.dispose();
|
|
564
|
+
disposeNamespaces?.();
|
|
565
|
+
}
|
|
566
|
+
};
|
|
141
567
|
}
|
|
142
568
|
const root = defineRoot(createCodemodeSession);
|
|
143
569
|
//#endregion
|
|
@@ -147,19 +573,45 @@ var root_server_default = defineRoot(root);
|
|
|
147
573
|
//#region src/extensions/workspaces/sessions/(backend)/contract/codemode.server.ts
|
|
148
574
|
var codemode_server_default = defineServiceContract(PO_CODEMODE_SERVICE, { scope: "session" });
|
|
149
575
|
//#endregion
|
|
150
|
-
//#region src/extensions/workspaces/sessions/(backend)/resource/
|
|
151
|
-
var
|
|
152
|
-
name: "
|
|
153
|
-
description: "
|
|
154
|
-
|
|
576
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-llm.server.ts
|
|
577
|
+
var codemode_how_to_llm_server_default = defineResource({
|
|
578
|
+
name: "codemode-how-to-llm",
|
|
579
|
+
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.",
|
|
580
|
+
pinned: true,
|
|
155
581
|
kind: "skill",
|
|
156
|
-
path: packageResourcePath(import.meta.url, "src/prompts/
|
|
157
|
-
read: () => readPackageResource(import.meta.url, "src/prompts/
|
|
582
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-llm/SKILL.md"),
|
|
583
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-llm/SKILL.md")
|
|
584
|
+
});
|
|
585
|
+
//#endregion
|
|
586
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-shell.server.ts
|
|
587
|
+
var codemode_how_to_shell_server_default = defineResource({
|
|
588
|
+
name: "codemode-how-to-shell",
|
|
589
|
+
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.",
|
|
590
|
+
pinned: true,
|
|
591
|
+
kind: "skill",
|
|
592
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-shell/SKILL.md"),
|
|
593
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-shell/SKILL.md")
|
|
594
|
+
});
|
|
595
|
+
//#endregion
|
|
596
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/how-to-codemode.server.ts
|
|
597
|
+
var how_to_codemode_server_default = defineResource({
|
|
598
|
+
name: "how-to-codemode",
|
|
599
|
+
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.",
|
|
600
|
+
pinned: true,
|
|
601
|
+
kind: "skill",
|
|
602
|
+
path: packageResourcePath(import.meta.url, "src/prompts/how-to-codemode/SKILL.md"),
|
|
603
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/how-to-codemode/SKILL.md")
|
|
158
604
|
});
|
|
159
605
|
//#endregion
|
|
160
606
|
//#region src/extensions/workspaces/sessions/(backend)/tool/discover.server.ts
|
|
161
607
|
var discover_server_default = defineServerTool((context) => context.root.discover);
|
|
162
608
|
//#endregion
|
|
609
|
+
//#region src/extensions/workspaces/sessions/(backend)/tool/exec_background.server.ts
|
|
610
|
+
var exec_background_server_default = defineServerTool((context) => context.root.execBackground);
|
|
611
|
+
//#endregion
|
|
612
|
+
//#region src/extensions/workspaces/sessions/(backend)/tool/exec_cancel.server.ts
|
|
613
|
+
var exec_cancel_server_default = defineServerTool((context) => context.root.execCancel);
|
|
614
|
+
//#endregion
|
|
163
615
|
//#region src/extensions/workspaces/sessions/(backend)/tool/exec.server.ts
|
|
164
616
|
var exec_server_default = defineServerTool((context) => context.root.exec);
|
|
165
617
|
//#endregion
|
|
@@ -183,10 +635,19 @@ const serverPlugin = defineServerPlugin({
|
|
|
183
635
|
return {
|
|
184
636
|
...composeRootHooks(scopeSession),
|
|
185
637
|
get resources() {
|
|
186
|
-
return [
|
|
638
|
+
return [
|
|
639
|
+
at(codemode_how_to_llm_server_default, contextSession),
|
|
640
|
+
at(codemode_how_to_shell_server_default, contextSession),
|
|
641
|
+
at(how_to_codemode_server_default, contextSession)
|
|
642
|
+
];
|
|
187
643
|
},
|
|
188
644
|
get tools() {
|
|
189
|
-
return [
|
|
645
|
+
return [
|
|
646
|
+
via({ name: "discover" }, at(discover_server_default, contextSession)),
|
|
647
|
+
via({ name: "exec_background" }, at(exec_background_server_default, contextSession)),
|
|
648
|
+
via({ name: "exec_cancel" }, at(exec_cancel_server_default, contextSession)),
|
|
649
|
+
via({ name: "exec" }, at(exec_server_default, contextSession))
|
|
650
|
+
];
|
|
190
651
|
},
|
|
191
652
|
services: [...scopeSession.services ?? []],
|
|
192
653
|
get activities() {
|