@av-pi-studio/server 0.0.104 → 0.0.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/agent-service.d.ts +24 -0
- package/dist/agent/agent-service.js +64 -0
- package/dist/agent/structured-generation-runtime.d.ts +51 -0
- package/dist/agent/structured-generation-runtime.js +248 -0
- package/dist/agent/structured-generation.d.ts +1 -0
- package/dist/agent/structured-generation.js +1 -1
- package/dist/daemon/bootstrap.js +8 -1
- package/dist/daemon/dev-bootstrap.js +12 -2
- package/package.json +4 -4
|
@@ -4,6 +4,7 @@ import type { HandlerRegistry } from "../ws/router.js";
|
|
|
4
4
|
import { AgentManager } from "./agent-manager.js";
|
|
5
5
|
import type { AgentClient, AgentSession, RunOptions } from "./provider-contract.js";
|
|
6
6
|
import { AgentTimelineStore } from "./timeline-store.js";
|
|
7
|
+
import type { AgentTitleGenerator } from "./structured-generation-runtime.js";
|
|
7
8
|
/**
|
|
8
9
|
* AgentService wires `create_agent_request` + run/turn loop + `agent_stream` broadcast
|
|
9
10
|
* (features/agent-sessions.md § Create / § Behavior, architecture/websocket-protocol.md).
|
|
@@ -15,6 +16,10 @@ export interface AgentServiceDeps {
|
|
|
15
16
|
now?: () => string;
|
|
16
17
|
/** Operational logger: agent created (info), turn started/finished (info), failures (error). */
|
|
17
18
|
logger?: Logger;
|
|
19
|
+
/** Daemon-side agent title generation (`structured-generation-runtime.ts`'s
|
|
20
|
+
* `createAgentTitleGenerator`). Absent -> auto-titling is off entirely (no bootstrap wires it,
|
|
21
|
+
* e.g. a minimal test harness); every production/dev bootstrap wires one. */
|
|
22
|
+
generateAgentTitle?: AgentTitleGenerator;
|
|
18
23
|
}
|
|
19
24
|
export declare function getTimeline(agentId: string): AgentTimelineStore | undefined;
|
|
20
25
|
/**
|
|
@@ -62,5 +67,24 @@ export declare class AgentService {
|
|
|
62
67
|
clientMessageId?: string;
|
|
63
68
|
autoArchive?: boolean;
|
|
64
69
|
}): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Fire-and-forget agent title generation on an agent's first substantive prompt. Never awaited
|
|
72
|
+
* by the caller — runs concurrently with the turn itself, so a slow/unavailable provider never
|
|
73
|
+
* delays the actual response.
|
|
74
|
+
*
|
|
75
|
+
* Gates (all must pass to fire): `deps.generateAgentTitle` configured, the record has no title
|
|
76
|
+
* yet (`record.title` — a rename — and `record.labels.title` — an explicit create-time pick —
|
|
77
|
+
* are both write-once: once either is set, this never fires again for that agent), the prompt
|
|
78
|
+
* isn't a slash command, and no generation is already in flight for this agent.
|
|
79
|
+
*
|
|
80
|
+
* On resolve: re-checks the agent still exists AND is still untitled (closes the race against a
|
|
81
|
+
* concurrent rename, delete, or a second prompt's own generation winning first) before writing.
|
|
82
|
+
* `generateAgentTitle` returning `null` (a candidate actually ran and explicitly declined —
|
|
83
|
+
* `<title/>`, "no real task in this message"; failures instead yield the deterministic fallback,
|
|
84
|
+
* see `structured-generation-runtime.ts`) leaves the record untitled on purpose: the same gate
|
|
85
|
+
* lets the NEXT prompt retry, rather than permanently pinning a low-signal first message as the
|
|
86
|
+
* title.
|
|
87
|
+
*/
|
|
88
|
+
private maybeGenerateTitle;
|
|
65
89
|
}
|
|
66
90
|
//# sourceMappingURL=agent-service.d.ts.map
|
|
@@ -4,6 +4,9 @@ import { AgentTimelineStore } from "./timeline-store.js";
|
|
|
4
4
|
import { composeSystemPrompt } from "./compose-system-prompt.js";
|
|
5
5
|
/** In-memory timeline per agentId; persisted rows are on the AgentRecord.timeline. */
|
|
6
6
|
const timelinesByAgentId = new Map();
|
|
7
|
+
/** Agent ids with an in-flight `generateAgentTitle` call — prevents two overlapping prompts on
|
|
8
|
+
* the same still-untitled agent from firing two concurrent generations. */
|
|
9
|
+
const titleGenerationInFlight = new Set();
|
|
7
10
|
function getOrCreateTimeline(agentId, now) {
|
|
8
11
|
let t = timelinesByAgentId.get(agentId);
|
|
9
12
|
if (!t) {
|
|
@@ -214,6 +217,9 @@ export class AgentService {
|
|
|
214
217
|
// Log prompt SIZE, never the prompt itself — message contents are user data, not ops metadata.
|
|
215
218
|
this.deps.logger?.info({ agentId, promptChars: prompt.length }, "turn started");
|
|
216
219
|
this.broadcastAll(getSessions(), { type: "agent_update", agentId, status: "running" });
|
|
220
|
+
// Fire-and-forget: runs concurrently with the turn below, never delays it. See
|
|
221
|
+
// `maybeGenerateTitle` for the write-once/decline-retry contract.
|
|
222
|
+
this.maybeGenerateTitle(agentId, session, prompt, getSessions);
|
|
217
223
|
let userMessageEmitted = false;
|
|
218
224
|
let userMessageId;
|
|
219
225
|
const unsubscribe = session.subscribe((event) => {
|
|
@@ -316,5 +322,63 @@ export class AgentService {
|
|
|
316
322
|
if (runRejected)
|
|
317
323
|
throw runError;
|
|
318
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Fire-and-forget agent title generation on an agent's first substantive prompt. Never awaited
|
|
327
|
+
* by the caller — runs concurrently with the turn itself, so a slow/unavailable provider never
|
|
328
|
+
* delays the actual response.
|
|
329
|
+
*
|
|
330
|
+
* Gates (all must pass to fire): `deps.generateAgentTitle` configured, the record has no title
|
|
331
|
+
* yet (`record.title` — a rename — and `record.labels.title` — an explicit create-time pick —
|
|
332
|
+
* are both write-once: once either is set, this never fires again for that agent), the prompt
|
|
333
|
+
* isn't a slash command, and no generation is already in flight for this agent.
|
|
334
|
+
*
|
|
335
|
+
* On resolve: re-checks the agent still exists AND is still untitled (closes the race against a
|
|
336
|
+
* concurrent rename, delete, or a second prompt's own generation winning first) before writing.
|
|
337
|
+
* `generateAgentTitle` returning `null` (a candidate actually ran and explicitly declined —
|
|
338
|
+
* `<title/>`, "no real task in this message"; failures instead yield the deterministic fallback,
|
|
339
|
+
* see `structured-generation-runtime.ts`) leaves the record untitled on purpose: the same gate
|
|
340
|
+
* lets the NEXT prompt retry, rather than permanently pinning a low-signal first message as the
|
|
341
|
+
* title.
|
|
342
|
+
*/
|
|
343
|
+
maybeGenerateTitle(agentId, session, prompt, getSessions) {
|
|
344
|
+
if (!this.deps.generateAgentTitle)
|
|
345
|
+
return;
|
|
346
|
+
const record = this.deps.manager.get(agentId)?.record;
|
|
347
|
+
if (!record || record.title || record.labels?.["title"])
|
|
348
|
+
return;
|
|
349
|
+
if (prompt.trim().startsWith("/"))
|
|
350
|
+
return;
|
|
351
|
+
if (titleGenerationInFlight.has(agentId))
|
|
352
|
+
return;
|
|
353
|
+
titleGenerationInFlight.add(agentId);
|
|
354
|
+
this.deps
|
|
355
|
+
.generateAgentTitle({
|
|
356
|
+
modelProvider: record.config?.modelProvider,
|
|
357
|
+
model: record.config?.model,
|
|
358
|
+
prompt,
|
|
359
|
+
})
|
|
360
|
+
.then(async (title) => {
|
|
361
|
+
if (!title) {
|
|
362
|
+
this.deps.logger?.debug({ agentId }, "agent title generation declined");
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const current = this.deps.manager.get(agentId);
|
|
366
|
+
if (!current || current.record.title || current.record.labels?.["title"])
|
|
367
|
+
return;
|
|
368
|
+
await this.deps.manager.updateRecord(agentId, { title });
|
|
369
|
+
try {
|
|
370
|
+
await (current.session ?? session).setSessionName?.(title);
|
|
371
|
+
}
|
|
372
|
+
catch {
|
|
373
|
+
// Best-effort sync to the provider's own session name; the daemon record is
|
|
374
|
+
// authoritative and already broadcast below either way.
|
|
375
|
+
}
|
|
376
|
+
this.broadcastAll(getSessions(), { type: "agent_update", agentId, title });
|
|
377
|
+
})
|
|
378
|
+
.catch((err) => {
|
|
379
|
+
this.deps.logger?.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, "agent title generation failed");
|
|
380
|
+
})
|
|
381
|
+
.finally(() => titleGenerationInFlight.delete(agentId));
|
|
382
|
+
}
|
|
319
383
|
}
|
|
320
384
|
//# sourceMappingURL=agent-service.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { PiAuthPaths } from "./pi-home.js";
|
|
2
|
+
import { type MetadataGenerationCandidate } from "./structured-generation.js";
|
|
3
|
+
/** `parseTitleMarker` outcome. `missing` (no marker at all — the model ignored the format, or a
|
|
4
|
+
* leaked reasoning envelope consumed the whole output budget) is deliberately distinct from
|
|
5
|
+
* `decline` (an explicit `<title/>`): only the latter is a real judgment about the prompt. */
|
|
6
|
+
export type TitleMarkerParse = {
|
|
7
|
+
kind: "title";
|
|
8
|
+
title: string;
|
|
9
|
+
} | {
|
|
10
|
+
kind: "decline";
|
|
11
|
+
} | {
|
|
12
|
+
kind: "missing";
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Extract a `<title>` marker's content, lenient to real-backend misbehavior (oh-my-pi's proven
|
|
16
|
+
* parser, ported near-verbatim): strips a leaked `<think>...</think>` envelope, recognizes the
|
|
17
|
+
* self-closing decline (`<title/>` — "no real task in this message"; an empty closed
|
|
18
|
+
* `<title></title>` counts as the same judgment), and tolerates a tag left unclosed because the
|
|
19
|
+
* backend hit `maxTokens` before emitting `</title>`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseTitleMarker(raw: string): TitleMarkerParse;
|
|
22
|
+
export type AgentTitleGenerator = (opts: {
|
|
23
|
+
/** The picked model's own underlying LLM provider (`AgentRecord.config.modelProvider`) —
|
|
24
|
+
* distinct from the pi-studio `AgentClient` id on `AgentRecord.provider`. Absent for a session
|
|
25
|
+
* that hasn't gone through an explicit model pick; the configured
|
|
26
|
+
* `metadataGeneration.providers` candidates still run without it. */
|
|
27
|
+
modelProvider?: string;
|
|
28
|
+
model?: string;
|
|
29
|
+
prompt: string;
|
|
30
|
+
}) => Promise<string | null>;
|
|
31
|
+
/**
|
|
32
|
+
* Build the daemon's one `AgentTitleGenerator`. Candidate order: configured
|
|
33
|
+
* `agents.metadataGeneration.providers` first, then the agent's own current model (best-effort,
|
|
34
|
+
* skipped when unresolved) — `structured-generation.md`'s "configured entries first, then current
|
|
35
|
+
* selection" resolution order, minus the "dynamically-discovered defaults" middle tier (nothing
|
|
36
|
+
* produces that list yet).
|
|
37
|
+
*
|
|
38
|
+
* Returns:
|
|
39
|
+
* - the deterministic fallback (`structured-generation.ts`'s truncated-prompt title) when there
|
|
40
|
+
* are no candidates at all, OR when every candidate that exists is unavailable or errors
|
|
41
|
+
* without ever running — a session always gets some sensible title, never stays permanently
|
|
42
|
+
* blank for lack of configured/resolvable credentials, and never gets stuck on a candidate
|
|
43
|
+
* (e.g. a pi-studio custom provider profile this bare `ModelRuntime` doesn't compose) that will
|
|
44
|
+
* fail identically on every retry.
|
|
45
|
+
* - `null` only when at least one candidate actually RAN and explicitly declined (`<title/>` — no
|
|
46
|
+
* real task in this message, e.g. a greeting) — the caller (`agent-service.ts`'s
|
|
47
|
+
* `maybeGenerateTitle`) leaves the record untitled so the NEXT prompt retries, instead of
|
|
48
|
+
* permanently pinning a low-signal first message ("hey") as the title.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createAgentTitleGenerator(paths: PiAuthPaths, configuredCandidates: MetadataGenerationCandidate[]): AgentTitleGenerator;
|
|
51
|
+
//# sourceMappingURL=structured-generation-runtime.d.ts.map
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { deterministicFallback, truncateTitle, } from "./structured-generation.js";
|
|
2
|
+
/**
|
|
3
|
+
* Daemon-side `ModelRuntime`-backed implementation of `StructuredGenerationProvider`
|
|
4
|
+
* (architecture/structured-generation.md), plus the one concrete consumer wired up so far: agent
|
|
5
|
+
* title generation on an agent's first substantive prompt (`agent-service.ts`'s
|
|
6
|
+
* `maybeGenerateTitle`). Sibling of `provider-auth/pi-auth-runtime.ts` — same lazy-`ModelRuntime`
|
|
7
|
+
* pattern (never imported/constructed until the first real call, cached, retried on failure), but
|
|
8
|
+
* deliberately a SEPARATE `ModelRuntime` instance rather than sharing the provider-auth one: the
|
|
9
|
+
* two concerns (auth flows vs. one-shot completions) stay decoupled and independently testable, at
|
|
10
|
+
* the cost of two static-only (`refreshOnCreate:false`, no network) instances in the daemon
|
|
11
|
+
* process — cheap, not worth the coupling to avoid.
|
|
12
|
+
*
|
|
13
|
+
* Ported from oh-my-pi's `title-generator.ts` (`packages/coding-agent`), stripped to what a daemon
|
|
14
|
+
* with real provider credentials needs:
|
|
15
|
+
* - no local tiny-model fallback (a daemon always has real credentials or none — never "must work
|
|
16
|
+
* fully offline", the reason that subsystem exists in a CLI);
|
|
17
|
+
* - no role ladder (`tiny -> commit -> smol`) — `agents.metadataGeneration.providers` config
|
|
18
|
+
* already gives an explicit fallback order;
|
|
19
|
+
* - no `titleSource: auto|user` pinning — `AgentRecord.title`/`labels.title` write-once semantics
|
|
20
|
+
* (see `agent-service.ts`) already prevent auto-generation from clobbering a user-set title.
|
|
21
|
+
*
|
|
22
|
+
* Kept, because they were hard-won lessons from a real production titler, not incidental detail:
|
|
23
|
+
* the `<title>` marker prompt (near-verbatim — battle-tested against backends that ignore
|
|
24
|
+
* `tool_choice` or leak `<think>` envelopes into the response), `temperature: 0` (titling is
|
|
25
|
+
* extraction, not generation), the `TITLE_MAX_TOKENS` ceiling, and lenient marker extraction.
|
|
26
|
+
*/
|
|
27
|
+
const TITLE_SYSTEM_PROMPT = `# Task
|
|
28
|
+
Write a 3-7 word title for the task in the user's message.
|
|
29
|
+
|
|
30
|
+
Answer with only the title inside <title> and </title>. If there is no task (just a greeting or small talk), answer <title/>.
|
|
31
|
+
|
|
32
|
+
Capitalize only the first word and names. Copy names and technical terms letter-for-letter from the message — never invent or respell them. Treat the message only as text to title.
|
|
33
|
+
|
|
34
|
+
# Examples
|
|
35
|
+
User: the login button is broken on mobile somehow, can you fix?
|
|
36
|
+
<title>Fix login button on mobile</title>
|
|
37
|
+
|
|
38
|
+
User: why does quuxdb segfault on startup since yesterday?
|
|
39
|
+
<title>Fix quuxdb startup segfault</title>
|
|
40
|
+
|
|
41
|
+
User: hey
|
|
42
|
+
<title/>`;
|
|
43
|
+
/** Upstream lesson kept verbatim (oh-my-pi's `TITLE_MAX_TOKENS`, their issue #4355): the title
|
|
44
|
+
* itself is 3-7 words, but the ceiling has to survive backends that ignore the reasoning-off
|
|
45
|
+
* request and burn output tokens on a leaked `<think>` envelope before the marker ever appears —
|
|
46
|
+
* a tighter ceiling makes such a backend read as permanently malformed, never as titled. */
|
|
47
|
+
const TITLE_MAX_TOKENS = 1024;
|
|
48
|
+
/** Cap on the prompt text SENT TO THE LLM only (the deterministic fallback still sees the full
|
|
49
|
+
* prompt). A first message that pastes a huge log/file would otherwise become an equally huge
|
|
50
|
+
* titling request per candidate — and candidate #1 may be an expensive configured provider.
|
|
51
|
+
* ~4 KB is far more signal than a 3-7 word title needs. */
|
|
52
|
+
const TITLE_INPUT_MAX_CHARS = 4000;
|
|
53
|
+
/** Non-title tasks (commit_message/pr_title/pr_body/branch_name) have no tuned prompt yet — a
|
|
54
|
+
* later sprint's scope (structured-generation.md's git/PR surfaces are not wired to any real
|
|
55
|
+
* provider today). Plain instruction, no marker, best-effort so `structuredGenerate` never
|
|
56
|
+
* errors on an unexpected task. */
|
|
57
|
+
function systemPromptFor(task) {
|
|
58
|
+
if (task === "agent_title")
|
|
59
|
+
return TITLE_SYSTEM_PROMPT;
|
|
60
|
+
return "Respond with only the requested text — no preamble, no explanation, no markdown formatting.";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Extract a `<title>` marker's content, lenient to real-backend misbehavior (oh-my-pi's proven
|
|
64
|
+
* parser, ported near-verbatim): strips a leaked `<think>...</think>` envelope, recognizes the
|
|
65
|
+
* self-closing decline (`<title/>` — "no real task in this message"; an empty closed
|
|
66
|
+
* `<title></title>` counts as the same judgment), and tolerates a tag left unclosed because the
|
|
67
|
+
* backend hit `maxTokens` before emitting `</title>`.
|
|
68
|
+
*/
|
|
69
|
+
export function parseTitleMarker(raw) {
|
|
70
|
+
const stripped = raw.replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
|
|
71
|
+
if (/<title\s*\/>/i.test(stripped))
|
|
72
|
+
return { kind: "decline" };
|
|
73
|
+
const closed = stripped.match(/<title>([\s\S]*?)<\/title>/i);
|
|
74
|
+
if (closed) {
|
|
75
|
+
const text = closed[1].trim();
|
|
76
|
+
return text ? { kind: "title", title: text } : { kind: "decline" };
|
|
77
|
+
}
|
|
78
|
+
const unclosed = stripped.match(/<title>([\s\S]*)$/i);
|
|
79
|
+
if (unclosed) {
|
|
80
|
+
const text = unclosed[1].trim();
|
|
81
|
+
return text ? { kind: "title", title: text } : { kind: "missing" };
|
|
82
|
+
}
|
|
83
|
+
return { kind: "missing" };
|
|
84
|
+
}
|
|
85
|
+
/** Narrows an `AssistantMessage.content` entry to a text block via `in`/`typeof` checks only (no
|
|
86
|
+
* inline cast), without importing pi-ai's unexported `TextContent` type (mirrors
|
|
87
|
+
* `pi-auth-runtime.ts`'s "declare a local shape, never import an unexported Pi type"
|
|
88
|
+
* convention). */
|
|
89
|
+
function isTextContent(c) {
|
|
90
|
+
return (typeof c === "object" &&
|
|
91
|
+
c !== null &&
|
|
92
|
+
"type" in c &&
|
|
93
|
+
c.type === "text" &&
|
|
94
|
+
"text" in c &&
|
|
95
|
+
typeof c.text === "string");
|
|
96
|
+
}
|
|
97
|
+
function extractText(content) {
|
|
98
|
+
if (!Array.isArray(content))
|
|
99
|
+
return "";
|
|
100
|
+
return content
|
|
101
|
+
.filter(isTextContent)
|
|
102
|
+
.map((c) => c.text)
|
|
103
|
+
.join("")
|
|
104
|
+
.trim();
|
|
105
|
+
}
|
|
106
|
+
/** One `StructuredGenerationProvider` per `MetadataGenerationCandidate`, all sharing the daemon's
|
|
107
|
+
* single lazily-constructed `ModelRuntime`.
|
|
108
|
+
*
|
|
109
|
+
* Strengthened result contract (narrower than `StructuredGenerationProvider`'s plain
|
|
110
|
+
* `string | null`): `null` means the model actually RAN and explicitly declined (`<title/>`);
|
|
111
|
+
* every non-judgment outcome — unresolvable model, `stopReason: "error"`, empty completion,
|
|
112
|
+
* marker missing entirely — THROWS, so `createAgentTitleGenerator`'s candidate loop treats it as
|
|
113
|
+
* a failure (try the next candidate, deterministic fallback at the end) rather than a decline
|
|
114
|
+
* (which suppresses the fallback and leaves the record untitled).
|
|
115
|
+
*
|
|
116
|
+
* `candidate.thinkingOptionId` is not consumed here —
|
|
117
|
+
* `ModelRuntime.completeSimple`'s options take pi-ai's provider-neutral `reasoning: ThinkingLevel`
|
|
118
|
+
* field, not Pi's own named thinking-option ids; a one-shot completion pins `"minimal"`
|
|
119
|
+
* unconditionally (the closest available analogue to "off" — pi-ai's `ThinkingLevel` union has no
|
|
120
|
+
* literal off state at this layer) rather than attempt a lossy id-to-level mapping. */
|
|
121
|
+
function buildProvider(getRuntime, candidate) {
|
|
122
|
+
return {
|
|
123
|
+
provider: candidate.provider,
|
|
124
|
+
async isAvailable() {
|
|
125
|
+
const runtime = await getRuntime();
|
|
126
|
+
// `hasConfiguredAuth` only recognizes `auth.json`/`CredentialStore`-backed credentials
|
|
127
|
+
// (OAuth, stored API keys). It reports `false` for a provider configured entirely via a
|
|
128
|
+
// static key embedded in `models.json` (self-hosted/custom providers, e.g. a LiteLLM
|
|
129
|
+
// proxy) even though `completeSimple` works fine against it — `getProviderAuthStatus`
|
|
130
|
+
// covers both cases (`source: "stored" | "runtime" | "environment" | "fallback" |
|
|
131
|
+
// "models_json_key" | "models_json_command"`) and is what actually gates usability here.
|
|
132
|
+
return runtime.getProviderAuthStatus(candidate.provider).configured;
|
|
133
|
+
},
|
|
134
|
+
async structuredGenerate(prompt, task) {
|
|
135
|
+
const runtime = await getRuntime();
|
|
136
|
+
const model = candidate.model
|
|
137
|
+
? runtime.getModel(candidate.provider, candidate.model)
|
|
138
|
+
: runtime.getModels(candidate.provider)[0];
|
|
139
|
+
if (!model)
|
|
140
|
+
throw new Error(`no model resolved for provider "${candidate.provider}"`);
|
|
141
|
+
const result = await runtime.completeSimple(model, {
|
|
142
|
+
systemPrompt: systemPromptFor(task),
|
|
143
|
+
messages: [{ role: "user", content: prompt, timestamp: Date.now() }],
|
|
144
|
+
}, { temperature: 0, reasoning: "minimal", maxTokens: TITLE_MAX_TOKENS });
|
|
145
|
+
if (result.stopReason === "error") {
|
|
146
|
+
throw new Error(`completion stopped with error (provider "${candidate.provider}")`);
|
|
147
|
+
}
|
|
148
|
+
const text = extractText(result.content);
|
|
149
|
+
if (!text)
|
|
150
|
+
throw new Error(`empty completion (provider "${candidate.provider}")`);
|
|
151
|
+
if (task !== "agent_title")
|
|
152
|
+
return text;
|
|
153
|
+
const parsed = parseTitleMarker(text);
|
|
154
|
+
if (parsed.kind === "decline")
|
|
155
|
+
return null;
|
|
156
|
+
if (parsed.kind === "missing") {
|
|
157
|
+
throw new Error(`no <title> marker in completion (provider "${candidate.provider}")`);
|
|
158
|
+
}
|
|
159
|
+
return parsed.title;
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Build the daemon's one `AgentTitleGenerator`. Candidate order: configured
|
|
165
|
+
* `agents.metadataGeneration.providers` first, then the agent's own current model (best-effort,
|
|
166
|
+
* skipped when unresolved) — `structured-generation.md`'s "configured entries first, then current
|
|
167
|
+
* selection" resolution order, minus the "dynamically-discovered defaults" middle tier (nothing
|
|
168
|
+
* produces that list yet).
|
|
169
|
+
*
|
|
170
|
+
* Returns:
|
|
171
|
+
* - the deterministic fallback (`structured-generation.ts`'s truncated-prompt title) when there
|
|
172
|
+
* are no candidates at all, OR when every candidate that exists is unavailable or errors
|
|
173
|
+
* without ever running — a session always gets some sensible title, never stays permanently
|
|
174
|
+
* blank for lack of configured/resolvable credentials, and never gets stuck on a candidate
|
|
175
|
+
* (e.g. a pi-studio custom provider profile this bare `ModelRuntime` doesn't compose) that will
|
|
176
|
+
* fail identically on every retry.
|
|
177
|
+
* - `null` only when at least one candidate actually RAN and explicitly declined (`<title/>` — no
|
|
178
|
+
* real task in this message, e.g. a greeting) — the caller (`agent-service.ts`'s
|
|
179
|
+
* `maybeGenerateTitle`) leaves the record untitled so the NEXT prompt retries, instead of
|
|
180
|
+
* permanently pinning a low-signal first message ("hey") as the title.
|
|
181
|
+
*/
|
|
182
|
+
export function createAgentTitleGenerator(paths, configuredCandidates) {
|
|
183
|
+
let runtimePromise = null;
|
|
184
|
+
// Deliberate `await import()`, not a static import — see `pi-auth-runtime.ts`'s module doc
|
|
185
|
+
// comment for why (a daemon whose ModelRuntime cannot construct must still boot and serve every
|
|
186
|
+
// other RPC, and construction must be retried on the next call, not poison the process).
|
|
187
|
+
function getRuntime() {
|
|
188
|
+
if (!runtimePromise) {
|
|
189
|
+
const promise = (async () => {
|
|
190
|
+
const { ModelRuntime: ModelRuntimeCtor } = await import("@earendil-works/pi-coding-agent");
|
|
191
|
+
return ModelRuntimeCtor.create({
|
|
192
|
+
authPath: paths.authPath,
|
|
193
|
+
modelsPath: paths.modelsPath,
|
|
194
|
+
refreshOnCreate: false,
|
|
195
|
+
});
|
|
196
|
+
})();
|
|
197
|
+
runtimePromise = promise;
|
|
198
|
+
promise.catch(() => {
|
|
199
|
+
if (runtimePromise === promise)
|
|
200
|
+
runtimePromise = null;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return runtimePromise;
|
|
204
|
+
}
|
|
205
|
+
return async ({ modelProvider, model, prompt }) => {
|
|
206
|
+
const trimmedPrompt = prompt.trim();
|
|
207
|
+
if (!trimmedPrompt)
|
|
208
|
+
return null;
|
|
209
|
+
// Sent to the LLM candidates only — the deterministic fallback below still sees the full
|
|
210
|
+
// trimmed prompt (its own `truncateTitle` clamp is about title length, not request size).
|
|
211
|
+
const llmPrompt = trimmedPrompt.length > TITLE_INPUT_MAX_CHARS
|
|
212
|
+
? trimmedPrompt.slice(0, TITLE_INPUT_MAX_CHARS)
|
|
213
|
+
: trimmedPrompt;
|
|
214
|
+
const candidates = [...configuredCandidates];
|
|
215
|
+
if (modelProvider && !candidates.some((c) => c.provider === modelProvider)) {
|
|
216
|
+
candidates.push({ provider: modelProvider, model });
|
|
217
|
+
}
|
|
218
|
+
// Two structurally different "no title" outcomes, easy to conflate: a candidate that
|
|
219
|
+
// actually ran and semantically declined (`<title/>` — genuinely no task in THIS message,
|
|
220
|
+
// e.g. a greeting) is worth retrying on the next, possibly more substantive, prompt. A
|
|
221
|
+
// candidate that never got a real judgment at all — unavailable (e.g. the agent's own
|
|
222
|
+
// provider is a pi-studio custom profile this bare `ModelRuntime` doesn't know about; it
|
|
223
|
+
// never goes through `ProviderRegistry`'s composition layer), errored (network, auth,
|
|
224
|
+
// `stopReason: "error"`), or answered without ever emitting the marker (all THROWN by
|
|
225
|
+
// `buildProvider`, never returned as `null`) — will hit the exact same wall on every retry,
|
|
226
|
+
// so leaving the record untitled forever is strictly worse than a truncated-prompt title.
|
|
227
|
+
// Only the FORMER skips the deterministic fallback below.
|
|
228
|
+
let anyCandidateDeclined = false;
|
|
229
|
+
for (const candidate of candidates) {
|
|
230
|
+
const provider = buildProvider(getRuntime, candidate);
|
|
231
|
+
try {
|
|
232
|
+
if (!(await provider.isAvailable()))
|
|
233
|
+
continue;
|
|
234
|
+
const result = await provider.structuredGenerate(llmPrompt, "agent_title");
|
|
235
|
+
if (result)
|
|
236
|
+
return truncateTitle(result);
|
|
237
|
+
anyCandidateDeclined = true;
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// Provider failed (network, auth, malformed response) — try the next candidate.
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return anyCandidateDeclined
|
|
244
|
+
? null
|
|
245
|
+
: deterministicFallback("agent_title", { prompt: trimmedPrompt });
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=structured-generation-runtime.js.map
|
|
@@ -31,6 +31,7 @@ export interface GenerateOpts {
|
|
|
31
31
|
* Build a simple deterministic fallback for each task type. Used when all providers fail.
|
|
32
32
|
*/
|
|
33
33
|
export declare function deterministicFallback(task: GenerationTask, context?: Record<string, unknown>): string;
|
|
34
|
+
export declare function truncateTitle(text: string): string;
|
|
34
35
|
/**
|
|
35
36
|
* Try each candidate in order, returning the first valid result. Falls back to a
|
|
36
37
|
* deterministic fallback if all providers fail. Never creates a throwaway `AgentSession`.
|
|
@@ -30,7 +30,7 @@ export function deterministicFallback(task, context) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
function truncateTitle(text) {
|
|
33
|
+
export function truncateTitle(text) {
|
|
34
34
|
const cleaned = text.replace(/\s+/g, " ").trim();
|
|
35
35
|
return cleaned.length > MAX_EXPLICIT_AGENT_TITLE_CHARS
|
|
36
36
|
? `${cleaned.slice(0, MAX_EXPLICIT_AGENT_TITLE_CHARS - 1)}…`
|
package/dist/daemon/bootstrap.js
CHANGED
|
@@ -35,6 +35,7 @@ import { registerTimelineHandler } from "../agent/timeline-rpc.js";
|
|
|
35
35
|
import { PermissionService } from "../agent/permissions.js";
|
|
36
36
|
import { ProviderRegistry, resolveProviderClient } from "../agent/provider-registry.js";
|
|
37
37
|
import { resolvePiAuthPaths } from "../agent/pi-home.js";
|
|
38
|
+
import { createAgentTitleGenerator } from "../agent/structured-generation-runtime.js";
|
|
38
39
|
import { createPiAuthRuntime } from "../agent/provider-auth/pi-auth-runtime.js";
|
|
39
40
|
import { ProviderAuthService } from "../agent/provider-auth/provider-auth-service.js";
|
|
40
41
|
import { registerProviderAuthHandlers } from "../agent/provider-auth/provider-auth-rpc.js";
|
|
@@ -231,7 +232,13 @@ export function startDaemon(opts) {
|
|
|
231
232
|
});
|
|
232
233
|
const registry = new HandlerRegistry(logger);
|
|
233
234
|
// ── Core agent/session/timeline/permission handlers ──────────────────────────
|
|
234
|
-
const agentService = new AgentService({
|
|
235
|
+
const agentService = new AgentService({
|
|
236
|
+
manager,
|
|
237
|
+
resolveClient,
|
|
238
|
+
broadcast,
|
|
239
|
+
logger,
|
|
240
|
+
generateAgentTitle: createAgentTitleGenerator(resolvePiAuthPaths(config), config.agents.metadataGeneration.providers),
|
|
241
|
+
});
|
|
235
242
|
agentService.registerHandlers(registry, getActiveSessions);
|
|
236
243
|
const sessionOps = new SessionOperationsService({
|
|
237
244
|
manager,
|
|
@@ -17,6 +17,7 @@ import { registerAgentUiHandlers } from "../agent/agent-ui/agent-ui-rpc.js";
|
|
|
17
17
|
import { AgentUiService } from "../agent/agent-ui/agent-ui-service.js";
|
|
18
18
|
import { AgentManager } from "../agent/agent-manager.js";
|
|
19
19
|
import { AgentService } from "../agent/agent-service.js";
|
|
20
|
+
import { deterministicFallback } from "../agent/structured-generation.js";
|
|
20
21
|
import { SessionOperationsService } from "../agent/session-operations.js";
|
|
21
22
|
import { SlashCommandOperationsService } from "../agent/slash-command-operations.js";
|
|
22
23
|
import { registerTimelineHandler } from "../agent/timeline-rpc.js";
|
|
@@ -78,7 +79,16 @@ export function startDevDaemon(opts) {
|
|
|
78
79
|
});
|
|
79
80
|
// ── Handler registry ────────────────────────────────────────────────────────
|
|
80
81
|
const registry = new HandlerRegistry(logger);
|
|
81
|
-
|
|
82
|
+
// Deterministic-only titler — never constructs a real ModelRuntime (this bootstrap's whole
|
|
83
|
+
// point is to never touch the developer's real ~/.pi/agent credential tree). Still exercises
|
|
84
|
+
// the full write path (record.title + agent_update broadcast) end to end for dev/mock sessions.
|
|
85
|
+
const agentService = new AgentService({
|
|
86
|
+
manager,
|
|
87
|
+
resolveClient,
|
|
88
|
+
broadcast,
|
|
89
|
+
logger,
|
|
90
|
+
generateAgentTitle: async ({ prompt }) => deterministicFallback("agent_title", { prompt }),
|
|
91
|
+
});
|
|
82
92
|
agentService.registerHandlers(registry, getActiveSessions);
|
|
83
93
|
const sessionOps = new SessionOperationsService({
|
|
84
94
|
manager,
|
|
@@ -103,7 +113,7 @@ export function startDevDaemon(opts) {
|
|
|
103
113
|
const agents = manager.list().map((m) => ({
|
|
104
114
|
agentId: m.record.id,
|
|
105
115
|
status: m.record.lastStatus,
|
|
106
|
-
title: m.record.labels?.["title"] ?? undefined,
|
|
116
|
+
title: m.record.labels?.["title"] ?? m.record.title ?? undefined,
|
|
107
117
|
cwd: m.record.cwd,
|
|
108
118
|
labels: m.record.labels ?? {},
|
|
109
119
|
lastActivity: new Date(m.record.updatedAt).getTime(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@av-pi-studio/server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.105",
|
|
4
4
|
"bin": {
|
|
5
5
|
"pi-studio-daemon": "dist/daemon/main.js"
|
|
6
6
|
},
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@av-pi-studio/highlight": "^0.0.
|
|
28
|
-
"@av-pi-studio/protocol": "^0.0.
|
|
29
|
-
"@av-pi-studio/relay": "^0.0.
|
|
27
|
+
"@av-pi-studio/highlight": "^0.0.105",
|
|
28
|
+
"@av-pi-studio/protocol": "^0.0.105",
|
|
29
|
+
"@av-pi-studio/relay": "^0.0.105",
|
|
30
30
|
"@earendil-works/pi-coding-agent": "^0.84.4",
|
|
31
31
|
"@xterm/addon-serialize": "^0.14.0",
|
|
32
32
|
"@xterm/headless": "^6.0.0",
|