omp-superagent 0.1.15 → 0.1.16

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.
@@ -0,0 +1,83 @@
1
+ import type { Inbound } from "../contracts";
2
+ import { matchesWrittenCommand, messengerCommandText } from "../messaging/messenger-commands";
3
+ import { record } from "./native-frames";
4
+
5
+ // Native command and branch journal contract from omp-superkit 12d19ec.
6
+ const selector = /^\s*(\/?superstack)(?=\s|$)/i;
7
+ export const headSuperstackTaskPrefix = "superstack\n\n";
8
+
9
+ type HeadSuperstackReceipt =
10
+ | { readonly kind: "enable" | "off" | "held" }
11
+ | { readonly kind: "task"; readonly start: number; readonly view: "written" | "telegram" };
12
+
13
+ // This runs only on original intake. For a task, pin an offset into one of
14
+ // the immutable source views rather than storing another copy of its text.
15
+ export function admitHeadSuperstack(input: Inbound, task: string): HeadSuperstackReceipt | undefined {
16
+ const match = selector.exec(task);
17
+ if (!match || !matchesWrittenCommand(input)) return;
18
+ const written = messengerCommandText(input);
19
+ if (!written.endsWith(task)) return;
20
+ const wrapperLength = written.length - task.length;
21
+ const start = wrapperLength + match[0].length;
22
+ const args = written.slice(start).trim();
23
+ const view = input.route.platform === "telegram" ? input.routingText : written;
24
+ const projected = view !== written;
25
+ if (projected) {
26
+ // An exposed second token on the same line cannot corroborate a masked
27
+ // first token. Telegram only attests the first written slash command.
28
+ const sameLine = /^[^\r\n]*/u.exec(written.slice(start))?.[0];
29
+ const attestedSlash = wrapperLength === 0 && match[1]?.startsWith("/") && input.telegramProtectedCommand === false;
30
+ if (view === undefined || !view.startsWith(written.slice(0, start)) || !/^(?:\s|$)/u.test(view.slice(start))
31
+ || (!attestedSlash && match[1] && sameLine?.includes(match[1]))) return { kind: "held" };
32
+ }
33
+ if (args === "" || args === "off")
34
+ return view?.slice(start).trim() === args ? { kind: args ? "off" : "enable" } : { kind: "held" };
35
+ if (!view?.slice(start).trim()) return { kind: "held" };
36
+ return { kind: "task", start, view: projected ? "telegram" : "written" };
37
+ }
38
+
39
+ export function recordedHeadSuperstack(value: string | null): HeadSuperstackReceipt | null {
40
+ if (value === null) return null;
41
+ const { kind, start, view } = record(JSON.parse(value));
42
+ if (kind === "task" && typeof start === "number" && Number.isSafeInteger(start) && start > 0
43
+ && (view === "written" || view === "telegram")) return { kind, start, view };
44
+ if (kind === "enable" || kind === "off") return { kind };
45
+ return { kind: "held" };
46
+ }
47
+
48
+ export function headSuperstackTaskText(input: Inbound, task: Extract<HeadSuperstackReceipt, { kind: "task" }>): string | undefined {
49
+ return (task.view === "telegram" ? input.routingText : messengerCommandText(input))?.slice(task.start).trim();
50
+ }
51
+
52
+ export function advertisesSuperstack(value: unknown): boolean {
53
+ const { commands } = record(value);
54
+ return Array.isArray(commands) && commands.map(record).some(command =>
55
+ command.name === "superstack" && command.source === "extension"
56
+ && command.description === "Enable branch-local skill routing for an optional task, or use off to disable it");
57
+ }
58
+
59
+ export function superstackBranchEnabled(value: unknown): boolean | undefined {
60
+ const { entries, leafId } = record(value);
61
+ if (!Array.isArray(entries) || (leafId !== null && typeof leafId !== "string")) return undefined;
62
+ const byId = new Map(entries.map(record).map(entry => [entry.id, entry]));
63
+ const visited = new Set<string>();
64
+ let id = leafId;
65
+ while (typeof id === "string" && !visited.has(id)) {
66
+ visited.add(id);
67
+ const entry = byId.get(id);
68
+ if (!entry) return undefined;
69
+ if (entry.type === "custom" && entry.customType === "omp-superstack:state")
70
+ return typeof entry.data === "boolean" ? entry.data : undefined;
71
+ if (entry.parentId !== null && typeof entry.parentId !== "string") return undefined;
72
+ id = entry.parentId;
73
+ }
74
+ return id === null ? false : undefined;
75
+ }
76
+
77
+ export function superstackControlOutcome(id: string, wanted: boolean, enabled: boolean | undefined) {
78
+ if (enabled === undefined) return { state: "uncertain",
79
+ text: `Superstack 설정을 확인하지 못했습니다. 명령을 다시 보내지 않았고 다음 HEAD 요청을 보류했습니다. 원래 세션을 확인하거나 /resolve ${id} 로 보류를 종료하세요.` } as const;
80
+ if (enabled !== wanted) return { state: "cancelled",
81
+ text: "HEAD 브랜치에 요청한 Superstack 설정이 적용되지 않았습니다. 모델 작업은 시작하지 않았습니다." } as const;
82
+ return { state: "done", text: `현재 HEAD 세션 브랜치의 Superstack 설정을 ${enabled ? "켰습니다" : "껐습니다"}. 모델 작업은 시작하지 않았습니다.` } as const;
83
+ }
@@ -5,6 +5,7 @@ import { getAgentDbPath } from "@oh-my-pi/pi-utils/dirs";
5
5
  import { authorizeRoute, type Config } from "../config/config";
6
6
  import type { InputRecord, Route, StorePort } from "../contracts";
7
7
  import { messengerCommandText, telegramCommandBlocked } from "../messaging/messenger-commands";
8
+ import { discordWorkThreadSource } from "../messaging/discord-work-thread";
8
9
  import { discardResultAttachment, isOriginalAttachmentMessage, pruneResultAttachments, readResultAttachmentIntent,
9
10
  readResultAttachmentStart, requestedResultPath, ResultAttachmentError, stageResultAttachment,
10
11
  type ResultAttachmentIntent, type ResultAttachmentPayload } from "../messaging/result-attachment";
@@ -56,7 +57,8 @@ export class ResultAttachments {
56
57
  if (route.authorityId !== this.config[route.platform]?.ownerId) {
57
58
  throw new ResultAttachmentError("Result attachments require the configured messenger owner");
58
59
  }
59
- if (!authorizeRoute(this.config, route)) throw new Error("Unauthorized messenger route");
60
+ if (!authorizeRoute(this.config, route) && !discordWorkThreadSource(this.config, this.store, route))
61
+ throw new Error("Unauthorized messenger route");
60
62
  }
61
63
 
62
64
  queue(run: AttachmentRun, intent: ResultAttachmentIntent, admission: {
@@ -36,6 +36,7 @@ import { compareMessageIds } from "../routing/message-order";
36
36
  import { storedWorkflowName, workflowChoice } from "../lane/workflow";
37
37
  import { laneWaitKey, type LaneReport } from "../lane/lane-report";
38
38
  import { laneWatchReplySource } from "../lane/lane-watch";
39
+ import { discordWorkThreadOwnerKey, recordedDiscordWorkThreadSource } from "../messaging/discord-work-thread";
39
40
  import { isResultAttachmentSnapshot } from "../messaging/result-attachment";
40
41
  import { existingWorkspace, TOPIC_CHOICE_WAIT_MS, topicRequest } from "../routing/topic-request";
41
42
  import type { CronUpdate } from "../cron/cron-jobs";
@@ -467,9 +468,27 @@ export class Store implements StorePort {
467
468
  return { lane, failed, original };
468
469
  }
469
470
 
471
+ private scopedWorkSource(input: InputRecord): { sourceId: string; owner: InputRecord } | null {
472
+ const ownerId = this.getMeta(discordWorkThreadOwnerKey(input.id));
473
+ if (ownerId === null) return null;
474
+ const sourceId = this.getMeta(`handoff-source:${input.id}`);
475
+ const source = sourceId && this.ownerSource(sourceId);
476
+ const placement = recordedDiscordWorkThreadSource(this, input.route);
477
+ if (!sourceId || !source || source.id !== ownerId || !placement
478
+ || routeKey(physicalRoute(source.route)) !== routeKey(placement)
479
+ || !input.target || input.target === "main" || this.laneForRoute(input.route)?.id !== input.target
480
+ || (input.id !== `handoff:${sourceId}:lane` && input.id !== `handoff:${sourceId}:${input.target}`
481
+ && (input.id !== `resume:${sourceId}:${input.target}` || this.getMeta(`native-goal-resume-owner:${input.id}`) === null))) {
482
+ throw new Error(`Invalid scoped Discord work-thread binding: ${input.id}`);
483
+ }
484
+ return { sourceId, owner: source };
485
+ }
486
+
470
487
  reportApprovalSource(id: string): InputRecord | null {
471
488
  const input = this.input(id);
472
489
  if (!input || isRetiredScopeInput(id)) return null;
490
+ const scoped = this.scopedWorkSource(input);
491
+ if (scoped) return scoped.owner;
473
492
  const reportId = input.target === "main" ? id : this.getMeta(`handoff-source:${id}`);
474
493
  if (!reportId || isRetiredScopeInput(reportId) || this.input(reportId)?.target !== "main") return null;
475
494
  const sourceId = this.getMeta(`report-approval-source:${reportId}`);
@@ -945,7 +964,10 @@ export class Store implements StorePort {
945
964
  if (!this.receive(input, target, {
946
965
  [`handoff-source:${input.id}`]: sourceId,
947
966
  [`native-goal-resume-owner:${input.id}`]: JSON.stringify(owner),
948
- }) || !this.submit(input.id, owner.requestId, owner.generation)) throw new Error("Native goal resume was not admitted");
967
+ ...(owner.scopedWorkOwnerId === undefined ? {} : { [discordWorkThreadOwnerKey(input.id)]: owner.scopedWorkOwnerId }),
968
+ })) throw new Error("Native goal resume was not admitted");
969
+ if (owner.scopedWorkOwnerId !== undefined) this.scopedWorkSource(this.requireInput(input.id));
970
+ if (!this.submit(input.id, owner.requestId, owner.generation)) throw new Error("Native goal resume was not admitted");
949
971
  return this.requireInput(input.id);
950
972
  }).immediate();
951
973
  }
@@ -1382,6 +1404,11 @@ export class Store implements StorePort {
1382
1404
  }
1383
1405
 
1384
1406
  laneReplyRoute(input: InputRecord, source: InputRecord): Route {
1407
+ const scoped = this.scopedWorkSource(input);
1408
+ if (scoped) {
1409
+ if (scoped.sourceId !== source.id) throw new Error(`Invalid scoped Discord reply source: ${input.id}`);
1410
+ return input.route;
1411
+ }
1385
1412
  const handoffReply = this.getMeta(`handoff-reply:${input.id}`);
1386
1413
  if (handoffReply !== null) {
1387
1414
  const { route } = input;