@pi-in-go/pigpen-ahp 0.1.0
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/CREDITS.md +27 -0
- package/LICENSE +23 -0
- package/README.md +148 -0
- package/extensions/ahp/access_options_test.go +116 -0
- package/extensions/ahp/extension.go +54 -0
- package/extensions/ahp/go.mod +12 -0
- package/extensions/ahp/go.sum +2 -0
- package/extensions/ahp/go.work +8 -0
- package/extensions/ahp/internal/channels/chat.go +110 -0
- package/extensions/ahp/internal/channels/root.go +32 -0
- package/extensions/ahp/internal/channels/session.go +128 -0
- package/extensions/ahp/internal/channels/session_test.go +108 -0
- package/extensions/ahp/internal/compose/compose.go +153 -0
- package/extensions/ahp/internal/compose/compose_test.go +120 -0
- package/extensions/ahp/internal/gaps/gaps_test.go +88 -0
- package/extensions/ahp/internal/host/handshake_test.go +206 -0
- package/extensions/ahp/internal/host/host.go +1305 -0
- package/extensions/ahp/internal/host/reconnect_test.go +197 -0
- package/extensions/ahp/internal/host/replaywindow_test.go +31 -0
- package/extensions/ahp/internal/host/schema_test.go +79 -0
- package/extensions/ahp/internal/host/store.go +291 -0
- package/extensions/ahp/internal/host/store_update_test.go +85 -0
- package/extensions/ahp/internal/host/subscriptions_test.go +160 -0
- package/extensions/ahp/internal/host/surface_test.go +95 -0
- package/extensions/ahp/internal/host/workarounds.go +458 -0
- package/extensions/ahp/internal/host/workarounds_test.go +96 -0
- package/extensions/ahp/internal/live/live.go +328 -0
- package/extensions/ahp/internal/live/live_test.go +91 -0
- package/extensions/ahp/internal/live/store.go +113 -0
- package/extensions/ahp/internal/mapper/activity.go +199 -0
- package/extensions/ahp/internal/mapper/activity_test.go +188 -0
- package/extensions/ahp/internal/mapper/bench_test.go +38 -0
- package/extensions/ahp/internal/mapper/event_mapper_test.go +380 -0
- package/extensions/ahp/internal/mapper/fixtures_test.go +387 -0
- package/extensions/ahp/internal/mapper/helpers_test.go +251 -0
- package/extensions/ahp/internal/mapper/js.go +131 -0
- package/extensions/ahp/internal/mapper/mapper.go +600 -0
- package/extensions/ahp/internal/mapper/message_input_test.go +196 -0
- package/extensions/ahp/internal/mapper/messageinput.go +272 -0
- package/extensions/ahp/internal/mapper/schema_helpers_test.go +5 -0
- package/extensions/ahp/internal/mapper/testdata/LICENSE-pi-ahp +21 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/abort.json +711 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/bash-long-output.json +1312 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/compaction.json +6285 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/parallel-tools.json +2622 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/plain-text.json +1856 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/single-tool.json +1128 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/steering.json +3926 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-bash.json +2372 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-edit.json +3983 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-error.json +2850 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-find.json +1162 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-grep.json +2783 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-loop.json +1693 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-ls.json +2427 -0
- package/extensions/ahp/internal/mapper/testdata/fixtures/tool-write.json +1271 -0
- package/extensions/ahp/internal/mapper/title.go +34 -0
- package/extensions/ahp/internal/mapper/usermsg.go +87 -0
- package/extensions/ahp/internal/pi/activeturn_test.go +296 -0
- package/extensions/ahp/internal/pi/backend.go +64 -0
- package/extensions/ahp/internal/pi/catalogue.go +370 -0
- package/extensions/ahp/internal/pi/catalogue_test.go +271 -0
- package/extensions/ahp/internal/pi/chatdriver.go +533 -0
- package/extensions/ahp/internal/pi/chatdriver_test.go +416 -0
- package/extensions/ahp/internal/pi/clientactions.go +212 -0
- package/extensions/ahp/internal/pi/clientactions_test.go +273 -0
- package/extensions/ahp/internal/pi/completions.go +230 -0
- package/extensions/ahp/internal/pi/completions_test.go +272 -0
- package/extensions/ahp/internal/pi/config_test.go +269 -0
- package/extensions/ahp/internal/pi/deletesession.go +69 -0
- package/extensions/ahp/internal/pi/deletesession_test.go +153 -0
- package/extensions/ahp/internal/pi/disposal_test.go +362 -0
- package/extensions/ahp/internal/pi/fixture_test.go +308 -0
- package/extensions/ahp/internal/pi/foreign_test.go +122 -0
- package/extensions/ahp/internal/pi/harness_test.go +83 -0
- package/extensions/ahp/internal/pi/history.go +269 -0
- package/extensions/ahp/internal/pi/hydrated_test.go +171 -0
- package/extensions/ahp/internal/pi/hydratedlifecycle_test.go +196 -0
- package/extensions/ahp/internal/pi/hydration_test.go +209 -0
- package/extensions/ahp/internal/pi/hydrator.go +212 -0
- package/extensions/ahp/internal/pi/imageinput.go +224 -0
- package/extensions/ahp/internal/pi/imageinput_test.go +57 -0
- package/extensions/ahp/internal/pi/lifecycle_test.go +381 -0
- package/extensions/ahp/internal/pi/models.go +212 -0
- package/extensions/ahp/internal/pi/models_test.go +128 -0
- package/extensions/ahp/internal/pi/paging.go +95 -0
- package/extensions/ahp/internal/pi/pagingtruncate_test.go +323 -0
- package/extensions/ahp/internal/pi/projecttrust.go +222 -0
- package/extensions/ahp/internal/pi/registry.go +924 -0
- package/extensions/ahp/internal/pi/restart_test.go +185 -0
- package/extensions/ahp/internal/pi/services.go +70 -0
- package/extensions/ahp/internal/pi/sessionconfig.go +64 -0
- package/extensions/ahp/internal/pi/sessionfiles_test.go +74 -0
- package/extensions/ahp/internal/pi/sessionstore.go +83 -0
- package/extensions/ahp/internal/pi/summary_test.go +426 -0
- package/extensions/ahp/internal/pi/workarounds_wire_test.go +208 -0
- package/extensions/ahp/internal/pisession/fromentries_test.go +32 -0
- package/extensions/ahp/internal/pisession/json.go +31 -0
- package/extensions/ahp/internal/pisession/pisession.go +580 -0
- package/extensions/ahp/internal/settings/access.go +63 -0
- package/extensions/ahp/internal/settings/access_test.go +52 -0
- package/extensions/ahp/internal/settings/settings.go +117 -0
- package/extensions/ahp/internal/settings/settings_test.go +104 -0
- package/extensions/ahp/internal/svc/etag_other.go +15 -0
- package/extensions/ahp/internal/svc/etag_unix.go +34 -0
- package/extensions/ahp/internal/svc/glob.go +197 -0
- package/extensions/ahp/internal/svc/mime.go +37 -0
- package/extensions/ahp/internal/svc/paths.go +108 -0
- package/extensions/ahp/internal/svc/pty.go +64 -0
- package/extensions/ahp/internal/svc/pty_darwin.go +42 -0
- package/extensions/ahp/internal/svc/pty_linux.go +36 -0
- package/extensions/ahp/internal/svc/pty_other.go +9 -0
- package/extensions/ahp/internal/svc/pty_test.go +114 -0
- package/extensions/ahp/internal/svc/pty_unix.go +147 -0
- package/extensions/ahp/internal/svc/resource.go +495 -0
- package/extensions/ahp/internal/svc/resource_test.go +501 -0
- package/extensions/ahp/internal/svc/stat_bsd.go +9 -0
- package/extensions/ahp/internal/svc/stat_linux.go +9 -0
- package/extensions/ahp/internal/svc/terminal.go +454 -0
- package/extensions/ahp/internal/svc/terminal_test.go +512 -0
- package/extensions/ahp/internal/svc/watch.go +590 -0
- package/extensions/ahp/internal/svc/watch_test.go +627 -0
- package/extensions/ahp/internal/svc/watchevents_test.go +452 -0
- package/extensions/ahp/internal/svc/watchpolicy.go +81 -0
- package/extensions/ahp/internal/svc/watchpolicy_test.go +151 -0
- package/extensions/ahp/internal/svc/watchracy_test.go +61 -0
- package/extensions/ahp/internal/testkit/schema/LICENSE-agent-host-protocol +21 -0
- package/extensions/ahp/internal/testkit/schema/actions.schema.json +9208 -0
- package/extensions/ahp/internal/testkit/schema/commands.schema.json +10862 -0
- package/extensions/ahp/internal/testkit/schema/errors.schema.json +10928 -0
- package/extensions/ahp/internal/testkit/schema/notifications.schema.json +6667 -0
- package/extensions/ahp/internal/testkit/schema/state.schema.json +6392 -0
- package/extensions/ahp/internal/testkit/schema.go +360 -0
- package/extensions/ahp/internal/testkit/testkit.go +367 -0
- package/extensions/ahp/internal/twin/twin.go +27 -0
- package/extensions/ahp/internal/wire/dispatchable.go +106 -0
- package/extensions/ahp/internal/wire/dispatchable_list.go +15 -0
- package/extensions/ahp/internal/wire/helpers_test.go +5 -0
- package/extensions/ahp/internal/wire/wire.go +218 -0
- package/extensions/ahp/internal/wire/wire_test.go +123 -0
- package/extensions/ahp/internal/ws/helpers_test.go +7 -0
- package/extensions/ahp/internal/ws/vscode_test.go +198 -0
- package/extensions/ahp/internal/ws/ws.go +674 -0
- package/extensions/ahp/internal/ws/ws_test.go +324 -0
- package/extensions/ahp/realpig_test.go +589 -0
- package/extensions/ahp/runtime.go +311 -0
- package/extensions/ahp/runtime_test.go +110 -0
- package/extensions/ahp/testdata/upstream-tests.json +3833 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/LICENSE +21 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/NOTICE-PIGPEN.md +18 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahp/client.go +1011 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahp/error.go +111 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahp/multi_host_state_mirror.go +239 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahp/reducers.go +1939 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahp/transport.go +176 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/actions.generated.go +2447 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/commands.generated.go +1546 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/common.go +205 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/errors.generated.go +65 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/messages.generated.go +138 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/notifications.generated.go +264 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/state.generated.go +6171 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/version.generated.go +31 -0
- package/extensions/ahp/third_party/agent-host-protocol-go/go.mod +3 -0
- package/extensions/ahp/twins_test.go +91 -0
- package/package.json +40 -0
- package/proof/PORT.md +121 -0
- package/proof/mutations.json +152 -0
- package/proof/oracle/LICENSE +21 -0
- package/proof/oracle/README.md +120 -0
- package/proof/oracle/UPSTREAM.md +7 -0
- package/proof/oracle/package.json +68 -0
- package/proof/oracle/src/bin/cli.ts +66 -0
- package/proof/oracle/src/bin/tunnel.ts +128 -0
- package/proof/oracle/src/channels/chat.ts +134 -0
- package/proof/oracle/src/channels/root.ts +39 -0
- package/proof/oracle/src/channels/session.ts +118 -0
- package/proof/oracle/src/channels/terminal.ts +11 -0
- package/proof/oracle/src/core/channels.ts +108 -0
- package/proof/oracle/src/core/client-workarounds.ts +358 -0
- package/proof/oracle/src/core/connection.ts +41 -0
- package/proof/oracle/src/core/host.ts +882 -0
- package/proof/oracle/src/core/sequencer.ts +75 -0
- package/proof/oracle/src/core/state-store.ts +162 -0
- package/proof/oracle/src/core/uri.ts +25 -0
- package/proof/oracle/src/host/direct-settings.ts +110 -0
- package/proof/oracle/src/host/pi-host.ts +220 -0
- package/proof/oracle/src/host/serve.ts +71 -0
- package/proof/oracle/src/host/terminal-service.ts +346 -0
- package/proof/oracle/src/pi/activity.ts +171 -0
- package/proof/oracle/src/pi/changeset-service.ts +646 -0
- package/proof/oracle/src/pi/changeset-uri.ts +64 -0
- package/proof/oracle/src/pi/chat-driver.ts +529 -0
- package/proof/oracle/src/pi/completions.ts +160 -0
- package/proof/oracle/src/pi/delete-session.ts +53 -0
- package/proof/oracle/src/pi/event-mapper.ts +648 -0
- package/proof/oracle/src/pi/git-changes.ts +605 -0
- package/proof/oracle/src/pi/history.ts +305 -0
- package/proof/oracle/src/pi/image-input.ts +56 -0
- package/proof/oracle/src/pi/image-mime.ts +8 -0
- package/proof/oracle/src/pi/in-process-backend.ts +155 -0
- package/proof/oracle/src/pi/message-input.ts +212 -0
- package/proof/oracle/src/pi/models.ts +124 -0
- package/proof/oracle/src/pi/project-trust.ts +66 -0
- package/proof/oracle/src/pi/provider.ts +2 -0
- package/proof/oracle/src/pi/resource-paths.ts +80 -0
- package/proof/oracle/src/pi/resource-service.ts +356 -0
- package/proof/oracle/src/pi/resource-watch-policy.ts +35 -0
- package/proof/oracle/src/pi/resource-watch.ts +357 -0
- package/proof/oracle/src/pi/session-catalogue.ts +318 -0
- package/proof/oracle/src/pi/session-config.ts +84 -0
- package/proof/oracle/src/pi/session-history.ts +75 -0
- package/proof/oracle/src/pi/session-hydrator.ts +219 -0
- package/proof/oracle/src/pi/session-registry.ts +815 -0
- package/proof/oracle/src/pi/session-storage.ts +29 -0
- package/proof/oracle/src/pi/session-title.ts +18 -0
- package/proof/oracle/src/pi/turn-paging.ts +94 -0
- package/proof/oracle/src/pi/user-message.ts +66 -0
- package/proof/oracle/src/protocol/errors.ts +44 -0
- package/proof/oracle/src/protocol/jsonrpc.ts +89 -0
- package/proof/oracle/src/protocol/version.ts +39 -0
- package/proof/oracle/src/transport/websocket.ts +136 -0
- package/proof/oracle/src/tunnel/devtunnel.ts +293 -0
- package/proof/oracle/src/tunnel/discovery.ts +37 -0
- package/proof/oracle/test/active-turn-reconnect.test.ts +274 -0
- package/proof/oracle/test/activity.test.ts +171 -0
- package/proof/oracle/test/changeset-lifecycle.test.serial.ts +303 -0
- package/proof/oracle/test/changeset-uri.test.ts +37 -0
- package/proof/oracle/test/changeset.test.serial.ts +487 -0
- package/proof/oracle/test/chat-driver.test.ts +691 -0
- package/proof/oracle/test/client-actions.test.ts +409 -0
- package/proof/oracle/test/client-workarounds.test.ts +320 -0
- package/proof/oracle/test/completions.test.ts +323 -0
- package/proof/oracle/test/delete-session.test.ts +142 -0
- package/proof/oracle/test/direct-settings.test.ts +81 -0
- package/proof/oracle/test/event-mapper.test.ts +619 -0
- package/proof/oracle/test/fetch-turns.test.ts +207 -0
- package/proof/oracle/test/fixtures/abort.json +711 -0
- package/proof/oracle/test/fixtures/bash-long-output.json +1312 -0
- package/proof/oracle/test/fixtures/compaction.json +6285 -0
- package/proof/oracle/test/fixtures/parallel-tools.json +2622 -0
- package/proof/oracle/test/fixtures/plain-text.json +1856 -0
- package/proof/oracle/test/fixtures/single-tool.json +1128 -0
- package/proof/oracle/test/fixtures/steering.json +3926 -0
- package/proof/oracle/test/fixtures/tool-bash.json +2372 -0
- package/proof/oracle/test/fixtures/tool-edit.json +3983 -0
- package/proof/oracle/test/fixtures/tool-error.json +2850 -0
- package/proof/oracle/test/fixtures/tool-find.json +1162 -0
- package/proof/oracle/test/fixtures/tool-grep.json +2783 -0
- package/proof/oracle/test/fixtures/tool-loop.json +1693 -0
- package/proof/oracle/test/fixtures/tool-ls.json +2427 -0
- package/proof/oracle/test/fixtures/tool-write.json +1271 -0
- package/proof/oracle/test/handshake.test.ts +275 -0
- package/proof/oracle/test/harness.ts +153 -0
- package/proof/oracle/test/hydrated-session-lifecycle.test.ts +211 -0
- package/proof/oracle/test/image-input.test.ts +28 -0
- package/proof/oracle/test/image-session.test.ts +69 -0
- package/proof/oracle/test/live-turn.test.ts +304 -0
- package/proof/oracle/test/mapper-fixtures.test.ts +293 -0
- package/proof/oracle/test/message-input.test.ts +181 -0
- package/proof/oracle/test/model-discovery.test.ts +170 -0
- package/proof/oracle/test/models.test.ts +109 -0
- package/proof/oracle/test/pi-host.test.ts +91 -0
- package/proof/oracle/test/pi-replay.test.ts +141 -0
- package/proof/oracle/test/project-trust.test.ts +125 -0
- package/proof/oracle/test/protocol-surface.test.ts +121 -0
- package/proof/oracle/test/pty.test.ts +100 -0
- package/proof/oracle/test/reconnect.test.ts +389 -0
- package/proof/oracle/test/resource-watch-policy.test.ts +78 -0
- package/proof/oracle/test/resource-watch.test.serial.ts +536 -0
- package/proof/oracle/test/resource.test.ts +499 -0
- package/proof/oracle/test/schema.test.ts +88 -0
- package/proof/oracle/test/session-catalogue.test.ts +314 -0
- package/proof/oracle/test/session-config.test.ts +224 -0
- package/proof/oracle/test/session-disposal.test.ts +364 -0
- package/proof/oracle/test/session-hydration.test.ts +194 -0
- package/proof/oracle/test/session-lifecycle.test.ts +416 -0
- package/proof/oracle/test/session-storage.test.serial.ts +32 -0
- package/proof/oracle/test/session-summary.test.ts +418 -0
- package/proof/oracle/test/subscriptions.test.ts +198 -0
- package/proof/oracle/test/support/assertions.ts +32 -0
- package/proof/oracle/test/support/async.ts +26 -0
- package/proof/oracle/test/support/hydrated-session.ts +205 -0
- package/proof/oracle/test/support/images.ts +8 -0
- package/proof/oracle/test/support/recorded-fixtures.ts +60 -0
- package/proof/oracle/test/support/recorded-scenarios.ts +145 -0
- package/proof/oracle/test/support/replay.ts +194 -0
- package/proof/oracle/test/support/schema.ts +221 -0
- package/proof/oracle/test/support/session-files.ts +12 -0
- package/proof/oracle/test/support/session-storage.ts +22 -0
- package/proof/oracle/test/support/upstream.ts +25 -0
- package/proof/oracle/test/support/watch-events.ts +108 -0
- package/proof/oracle/test/terminal-service.test.ts +495 -0
- package/proof/oracle/test/truncate.test.ts +230 -0
- package/proof/oracle/test/tunnel.test.ts +258 -0
- package/proof/oracle/test/upstream-workarounds.test.ts +37 -0
- package/proof/oracle/test/uri.test.ts +24 -0
- package/proof/oracle/test/watch-events.test.ts +118 -0
- package/proof/oracle/tsconfig.json +32 -0
- package/proof/proof-piglet/piglet.yaml +18 -0
- package/proof/tools/fakellm/go.mod +3 -0
- package/proof/tools/fakellm/main.go +109 -0
- package/proof/tools/gen-dispatchable.py +30 -0
- package/provenance.json +28 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `pi-ahp` — starts the host and prints the endpoint clients connect to.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { loadDirectListenerSettings, SettingsError } from "../host/direct-settings.ts";
|
|
7
|
+
import { closeOnSignal, startHost, VERSION } from "../host/serve.ts";
|
|
8
|
+
|
|
9
|
+
function log(message: string): void {
|
|
10
|
+
process.stderr.write(`${message}\n`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function flagValue(args: string[], flag: string): string | undefined {
|
|
14
|
+
const index = args.indexOf(flag);
|
|
15
|
+
return index >= 0 ? args[index + 1] : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function main(): Promise<void> {
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
21
|
+
log(
|
|
22
|
+
[
|
|
23
|
+
`pi-ahp v${VERSION}`,
|
|
24
|
+
"",
|
|
25
|
+
"Usage: pi-ahp [--port <n>] [--host <addr>] [--cwd <path>] [--verbose]",
|
|
26
|
+
"",
|
|
27
|
+
"Direct-listener settings persist at ~/.pi/ahp/settings.json; the",
|
|
28
|
+
"port and token are generated on first run.",
|
|
29
|
+
].join("\n"),
|
|
30
|
+
);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const verbose = args.includes("--verbose");
|
|
35
|
+
const settings = await loadDirectListenerSettings();
|
|
36
|
+
const workingDirectory = flagValue(args, "--cwd") ?? process.cwd();
|
|
37
|
+
|
|
38
|
+
const server = await startHost({
|
|
39
|
+
host: flagValue(args, "--host") ?? settings.host,
|
|
40
|
+
port: Number(flagValue(args, "--port") ?? settings.port),
|
|
41
|
+
connectionToken: settings.token ?? undefined,
|
|
42
|
+
workingDirectory,
|
|
43
|
+
...(verbose ? { log } : {}),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
log(
|
|
47
|
+
settings.token
|
|
48
|
+
? `pi-ahp listening on ws://${server.host}:${server.port}?token=${settings.token}`
|
|
49
|
+
: `pi-ahp listening on ws://${server.host}:${server.port} (no token — anyone who can reach ${server.host} can drive the agent)`,
|
|
50
|
+
);
|
|
51
|
+
log(`working directory: ${workingDirectory}`);
|
|
52
|
+
|
|
53
|
+
closeOnSignal(server);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
await main();
|
|
58
|
+
} catch (error) {
|
|
59
|
+
// A settings problem is the user's to fix, not a crash: print what is wrong
|
|
60
|
+
// and where, without a stack trace pointing into the store.
|
|
61
|
+
if (error instanceof SettingsError) {
|
|
62
|
+
process.stderr.write(`pi-ahp: ${error.message}\n`);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Serves pi-ahp in the fixed Dev Tunnel shape VS Code discovers. Dev Tunnels
|
|
4
|
+
* controls remote access; the forwarded loopback listener has no URL token.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { startHost, VERSION } from "../host/serve.ts";
|
|
9
|
+
import { DevTunnelCli, TunnelError } from "../tunnel/devtunnel.ts";
|
|
10
|
+
import { displayLabel, nameLabel, TUNNEL_PORT } from "../tunnel/discovery.ts";
|
|
11
|
+
|
|
12
|
+
function log(message: string): void {
|
|
13
|
+
process.stderr.write(`${message}\n`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function main(): Promise<void> {
|
|
17
|
+
const args = process.argv.slice(2);
|
|
18
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
19
|
+
log(
|
|
20
|
+
[
|
|
21
|
+
`pi-ahp-tunnel v${VERSION}`,
|
|
22
|
+
"",
|
|
23
|
+
"Usage: pi-ahp-tunnel [--name <label>] [--cwd <path>] [--verbose]",
|
|
24
|
+
"",
|
|
25
|
+
"Reuses this host's tunnel, creating one on the first run. `--name`",
|
|
26
|
+
"sets the display label shown by clients; without it, an existing",
|
|
27
|
+
"label is left alone.",
|
|
28
|
+
"",
|
|
29
|
+
"Serves the host over a dev tunnel that compatible AHP clients can",
|
|
30
|
+
"discover. It requires `devtunnel` on PATH and an existing login:",
|
|
31
|
+
"",
|
|
32
|
+
" devtunnel user login -g GitHub account",
|
|
33
|
+
" devtunnel user login Microsoft account (the default)",
|
|
34
|
+
"",
|
|
35
|
+
"Add -d for device-code auth when there is no browser. See",
|
|
36
|
+
"`devtunnel user login --help` for the rest.",
|
|
37
|
+
"",
|
|
38
|
+
"Does not read or modify pi-ahp's direct-listener settings. The",
|
|
39
|
+
`discovery port is fixed at ${TUNNEL_PORT}; remote access is controlled`,
|
|
40
|
+
"by Microsoft Dev Tunnels rather than an additional URL token.",
|
|
41
|
+
].join("\n"),
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const flag = (name: string): string | undefined => {
|
|
47
|
+
const index = args.indexOf(name);
|
|
48
|
+
return index >= 0 ? args[index + 1] : undefined;
|
|
49
|
+
};
|
|
50
|
+
const verbose = args.includes("--verbose");
|
|
51
|
+
const workingDirectory = flag("--cwd") ?? process.cwd();
|
|
52
|
+
|
|
53
|
+
const devtunnel = new DevTunnelCli();
|
|
54
|
+
devtunnel.requireLogin();
|
|
55
|
+
const requested = flag("--name");
|
|
56
|
+
const name = requested ? nameLabel(requested) : undefined;
|
|
57
|
+
if (requested && !name) {
|
|
58
|
+
log(`ignoring --name ${requested}: nothing left after removing characters a label cannot hold`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const existing = devtunnel.findTunnel();
|
|
62
|
+
let qualifiedId: string;
|
|
63
|
+
if (existing) {
|
|
64
|
+
qualifiedId = existing.tunnelId;
|
|
65
|
+
const current = displayLabel(existing.labels);
|
|
66
|
+
if (name && name !== current) {
|
|
67
|
+
devtunnel.rename(qualifiedId, current, name);
|
|
68
|
+
}
|
|
69
|
+
log(`tunnel: ${qualifiedId} (reused${name && name !== current ? `, renamed to ${name}` : ""})`);
|
|
70
|
+
} else {
|
|
71
|
+
qualifiedId = devtunnel.createTunnel(name);
|
|
72
|
+
log(`tunnel: ${qualifiedId} (new${name ? `, named ${name}` : ""})`);
|
|
73
|
+
}
|
|
74
|
+
devtunnel.ensurePort(qualifiedId);
|
|
75
|
+
|
|
76
|
+
// The host must be up before the tunnel forwards to it, or the first client
|
|
77
|
+
// through the relay hits a closed port.
|
|
78
|
+
const server = await startHost({
|
|
79
|
+
host: "127.0.0.1",
|
|
80
|
+
port: TUNNEL_PORT,
|
|
81
|
+
workingDirectory,
|
|
82
|
+
...(verbose ? { log } : {}),
|
|
83
|
+
});
|
|
84
|
+
log(`listening on 127.0.0.1:${server.port}, working directory: ${workingDirectory}`);
|
|
85
|
+
log("remote access control: Microsoft Dev Tunnels; local listener: no URL token");
|
|
86
|
+
|
|
87
|
+
const host = spawn("devtunnel", ["host", qualifiedId], { stdio: "inherit" });
|
|
88
|
+
let stopping = false;
|
|
89
|
+
let hostFinished = false;
|
|
90
|
+
|
|
91
|
+
// Shutting down goes through the child, not alongside it. Killing the child
|
|
92
|
+
// and exiting in the same tick raced: `devtunnel host` outlives us, keeps the
|
|
93
|
+
// relay connection, and the tunnel looks hosted by a process that is gone.
|
|
94
|
+
const finish = (code: number, failure?: string): void => {
|
|
95
|
+
if (hostFinished) return;
|
|
96
|
+
hostFinished = true;
|
|
97
|
+
if (!stopping && failure) log(failure);
|
|
98
|
+
void server.close().then(() => process.exit(stopping ? 0 : code));
|
|
99
|
+
};
|
|
100
|
+
host.on("exit", (code) => finish(code ?? 1, `devtunnel host exited (${code}); shutting down`));
|
|
101
|
+
host.on("error", (error) => finish(1, `devtunnel host failed: ${error.message}`));
|
|
102
|
+
|
|
103
|
+
const stop = (): void => {
|
|
104
|
+
if (stopping) {
|
|
105
|
+
// A second Ctrl-C means the wait itself is the problem.
|
|
106
|
+
process.exit(1);
|
|
107
|
+
}
|
|
108
|
+
stopping = true;
|
|
109
|
+
host.kill();
|
|
110
|
+
// A child that will not take SIGTERM should not wedge the terminal;
|
|
111
|
+
// an orphaned relay is the lesser failure.
|
|
112
|
+
setTimeout(() => host.kill("SIGKILL"), 5000).unref();
|
|
113
|
+
};
|
|
114
|
+
process.on("SIGINT", stop);
|
|
115
|
+
process.on("SIGTERM", stop);
|
|
116
|
+
log("");
|
|
117
|
+
log("Connect from a Dev Tunnel-capable AHP client signed in to the same account.");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await main();
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (error instanceof TunnelError) {
|
|
124
|
+
process.stderr.write(`pi-ahp-tunnel: ${error.message}\n`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The chat channel — `ahp-chat:/<uuid>`.
|
|
3
|
+
*
|
|
4
|
+
* Every session this host serves has exactly one chat. That is why the agent
|
|
5
|
+
* declares no `multipleChats` capability: its absence is the protocol's way of
|
|
6
|
+
* telling a client not to call `createChat`.
|
|
7
|
+
*
|
|
8
|
+
* @see https://microsoft.github.io/agent-host-protocol/specification/chat-channel
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
ActionType,
|
|
13
|
+
ChatOriginKind,
|
|
14
|
+
type ChatState,
|
|
15
|
+
type ChatSummary,
|
|
16
|
+
MessageKind,
|
|
17
|
+
type ModelSelection,
|
|
18
|
+
type SessionState,
|
|
19
|
+
SessionStatus,
|
|
20
|
+
type URI,
|
|
21
|
+
} from "@microsoft/agent-host-protocol";
|
|
22
|
+
import { chatUri } from "../core/channels.ts";
|
|
23
|
+
import type { AhpHost } from "../core/host.ts";
|
|
24
|
+
|
|
25
|
+
export function initialChatState(uri: URI, title: string, selection?: ModelSelection): ChatState {
|
|
26
|
+
return {
|
|
27
|
+
resource: uri,
|
|
28
|
+
title,
|
|
29
|
+
status: SessionStatus.Idle,
|
|
30
|
+
modifiedAt: new Date().toISOString(),
|
|
31
|
+
// A session's default chat exists because the user made the session.
|
|
32
|
+
origin: { kind: ChatOriginKind.User },
|
|
33
|
+
turns: [],
|
|
34
|
+
// Seeded before the agent exists. Starting one takes seconds, and a
|
|
35
|
+
// client that subscribes in the meantime would otherwise find an empty
|
|
36
|
+
// model picker and be unable to send. The backend refines this once it
|
|
37
|
+
// is up, if it resolves to something different.
|
|
38
|
+
...(selection ? { draft: { text: "", origin: { kind: MessageKind.User }, model: selection } } : {}),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function chatSummaryOf(state: ChatState): ChatSummary {
|
|
43
|
+
return {
|
|
44
|
+
resource: state.resource,
|
|
45
|
+
title: state.title,
|
|
46
|
+
status: state.status,
|
|
47
|
+
modifiedAt: state.modifiedAt,
|
|
48
|
+
...(state.activity !== undefined ? { activity: state.activity } : {}),
|
|
49
|
+
...(state.origin !== undefined ? { origin: state.origin } : {}),
|
|
50
|
+
...(state.interactivity !== undefined ? { interactivity: state.interactivity } : {}),
|
|
51
|
+
...(state.workingDirectories !== undefined ? { workingDirectories: state.workingDirectories } : {}),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Creates a session's default chat and registers it in the session catalog.
|
|
57
|
+
*
|
|
58
|
+
* The chat id is derived from the session id so that the pairing is
|
|
59
|
+
* reconstructible without a lookup table, matching how the session URI already
|
|
60
|
+
* carries pi's session id.
|
|
61
|
+
*/
|
|
62
|
+
export function installDefaultChat(
|
|
63
|
+
host: AhpHost,
|
|
64
|
+
sessionChannel: URI,
|
|
65
|
+
sessionId: string,
|
|
66
|
+
title: string,
|
|
67
|
+
selection?: ModelSelection,
|
|
68
|
+
): URI {
|
|
69
|
+
const uri = chatUri(sessionId);
|
|
70
|
+
host.store.create(uri, initialChatState(uri, title, selection));
|
|
71
|
+
|
|
72
|
+
const summary = chatSummaryOf(host.store.get(uri) as ChatState);
|
|
73
|
+
host.dispatchServerAction(sessionChannel, { type: ActionType.SessionChatAdded, summary });
|
|
74
|
+
host.dispatchServerAction(sessionChannel, { type: ActionType.SessionDefaultChatChanged, defaultChat: uri });
|
|
75
|
+
return uri;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function equalUris(left: readonly URI[] | undefined, right: readonly URI[] | undefined): boolean {
|
|
79
|
+
if (!left || !right) {
|
|
80
|
+
return left === right;
|
|
81
|
+
}
|
|
82
|
+
return left.length === right.length && left.every((uri, index) => uri === right[index]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function equalSummary(left: ChatSummary, right: ChatSummary): boolean {
|
|
86
|
+
return (
|
|
87
|
+
left.resource === right.resource &&
|
|
88
|
+
left.title === right.title &&
|
|
89
|
+
left.status === right.status &&
|
|
90
|
+
left.activity === right.activity &&
|
|
91
|
+
left.modifiedAt === right.modifiedAt &&
|
|
92
|
+
(left.origin === right.origin || JSON.stringify(left.origin) === JSON.stringify(right.origin)) &&
|
|
93
|
+
left.interactivity === right.interactivity &&
|
|
94
|
+
equalUris(left.workingDirectories, right.workingDirectories)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Mirrors a chat's denormalized summary fields into its owning session.
|
|
100
|
+
*
|
|
101
|
+
* Partial updates cannot express removal of an optional JSON field: an
|
|
102
|
+
* `undefined` value disappears on the wire and means "unchanged". Use the
|
|
103
|
+
* action's full-summary upsert form when one of those fields is cleared.
|
|
104
|
+
*/
|
|
105
|
+
export function syncChatSummary(host: AhpHost, sessionChannel: URI, chatChannel: URI): boolean {
|
|
106
|
+
const chat = host.store.get(chatChannel) as ChatState | undefined;
|
|
107
|
+
const session = host.store.get(sessionChannel) as SessionState | undefined;
|
|
108
|
+
const previous = session?.chats.find((summary) => summary.resource === chatChannel);
|
|
109
|
+
if (!chat || !previous) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const current = chatSummaryOf(chat);
|
|
114
|
+
if (equalSummary(previous, current)) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const clearsOptional =
|
|
119
|
+
(previous.activity !== undefined && current.activity === undefined) ||
|
|
120
|
+
(previous.origin !== undefined && current.origin === undefined) ||
|
|
121
|
+
(previous.interactivity !== undefined && current.interactivity === undefined) ||
|
|
122
|
+
(previous.workingDirectories !== undefined && current.workingDirectories === undefined);
|
|
123
|
+
if (clearsOptional) {
|
|
124
|
+
host.dispatchServerAction(sessionChannel, { type: ActionType.SessionChatAdded, summary: current });
|
|
125
|
+
} else {
|
|
126
|
+
const { resource: _resource, ...changes } = current;
|
|
127
|
+
host.dispatchServerAction(sessionChannel, {
|
|
128
|
+
type: ActionType.SessionChatUpdated,
|
|
129
|
+
chat: chatChannel,
|
|
130
|
+
changes,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The root channel — `ahp-root://`.
|
|
3
|
+
*
|
|
4
|
+
* Exactly one root channel exists per host. It carries the agents this host can
|
|
5
|
+
* speak to plus host-level config. The session catalogue is deliberately *not*
|
|
6
|
+
* root state: clients fetch it with `listSessions` and patch it from
|
|
7
|
+
* `root/session*` notifications.
|
|
8
|
+
*
|
|
9
|
+
* @see https://microsoft.github.io/agent-host-protocol/specification/root-channel
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { AgentInfo, RootState, SessionSummary, URI } from "@microsoft/agent-host-protocol";
|
|
13
|
+
import { ROOT_CHANNEL } from "../core/channels.ts";
|
|
14
|
+
import type { AhpHost } from "../core/host.ts";
|
|
15
|
+
|
|
16
|
+
function initialRootState(agents: AgentInfo[] = []): RootState {
|
|
17
|
+
return { agents, activeSessions: 0 };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Registers `ahp-root://` on the host. Must run before any client connects. */
|
|
21
|
+
export function installRootChannel(host: AhpHost, agents: AgentInfo[] = []): void {
|
|
22
|
+
host.store.create(ROOT_CHANNEL, initialRootState(agents));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── Catalogue notifications ───────────────────────────────────────────────
|
|
26
|
+
// Ephemeral and never replayed on reconnect; after reconnecting a client
|
|
27
|
+
// re-fetches the catalogue with `listSessions`.
|
|
28
|
+
|
|
29
|
+
export function notifySessionAdded(host: AhpHost, summary: SessionSummary): void {
|
|
30
|
+
host.notify(ROOT_CHANNEL, "root/sessionAdded", { summary });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function notifySessionRemoved(host: AhpHost, session: URI): void {
|
|
34
|
+
host.notify(ROOT_CHANNEL, "root/sessionRemoved", { session });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function notifySessionSummaryChanged(host: AhpHost, session: URI, changes: Partial<SessionSummary>): void {
|
|
38
|
+
host.notify(ROOT_CHANNEL, "root/sessionSummaryChanged", { session, changes });
|
|
39
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session channel — `ahp-session:/<uuid>`.
|
|
3
|
+
*
|
|
4
|
+
* A session is the coordination scope; the conversation itself lives on a chat
|
|
5
|
+
* channel underneath it. Creation is asynchronous by design: the host returns
|
|
6
|
+
* immediately with `lifecycle: 'creating'` and dispatches `session/ready` or
|
|
7
|
+
* `session/creationFailed` once backend startup settles, so a slow agent start
|
|
8
|
+
* never blocks the client's round-trip.
|
|
9
|
+
*
|
|
10
|
+
* @see https://microsoft.github.io/agent-host-protocol/specification/session-channel
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type ChatSummary,
|
|
15
|
+
SessionLifecycle,
|
|
16
|
+
type SessionState,
|
|
17
|
+
SessionStatus,
|
|
18
|
+
type SessionSummary,
|
|
19
|
+
type URI,
|
|
20
|
+
} from "@microsoft/agent-host-protocol";
|
|
21
|
+
import { pathToFileUri } from "../core/uri.ts";
|
|
22
|
+
|
|
23
|
+
export function initialSessionState(provider: string, title: string, workingDirectory: string): SessionState {
|
|
24
|
+
return {
|
|
25
|
+
provider,
|
|
26
|
+
title,
|
|
27
|
+
status: SessionStatus.Idle,
|
|
28
|
+
lifecycle: SessionLifecycle.Creating,
|
|
29
|
+
workingDirectories: [pathToFileUri(workingDirectory)],
|
|
30
|
+
activeClients: [],
|
|
31
|
+
chats: [],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The low five bits encode activity; higher bits carry independent metadata flags. */
|
|
36
|
+
const STATUS_ACTIVITY_MASK = (1 << 5) - 1;
|
|
37
|
+
|
|
38
|
+
interface SessionChatAggregate {
|
|
39
|
+
readonly status: SessionStatus;
|
|
40
|
+
readonly activity?: string;
|
|
41
|
+
readonly modifiedAt?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function timestamp(summary: ChatSummary): number {
|
|
45
|
+
const value = Date.parse(summary.modifiedAt);
|
|
46
|
+
return Number.isFinite(value) ? value : Number.NEGATIVE_INFINITY;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function mostRecent(chats: readonly ChatSummary[], matches: (chat: ChatSummary) => boolean): ChatSummary | undefined {
|
|
50
|
+
let latest: ChatSummary | undefined;
|
|
51
|
+
for (const chat of chats) {
|
|
52
|
+
if (matches(chat) && (!latest || timestamp(chat) > timestamp(latest))) {
|
|
53
|
+
latest = chat;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return latest;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Projects the session-level summary fields AHP derives from its chat catalog.
|
|
61
|
+
*
|
|
62
|
+
* The current host exposes one chat, so this is a direct pass-through today.
|
|
63
|
+
* Keeping the complete 0.9 rule here makes the projection deterministic and
|
|
64
|
+
* avoids teaching the pi adapter about protocol status bits.
|
|
65
|
+
*/
|
|
66
|
+
export function aggregateSessionChats(state: SessionState): SessionChatAggregate {
|
|
67
|
+
if (state.chats.length === 0) {
|
|
68
|
+
return {
|
|
69
|
+
status: state.status,
|
|
70
|
+
...(state.activity !== undefined ? { activity: state.activity } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const latest = mostRecent(state.chats, () => true);
|
|
75
|
+
if (!latest) {
|
|
76
|
+
return { status: state.status };
|
|
77
|
+
}
|
|
78
|
+
let driver = state.chats.find((chat) => chat.resource === state.defaultChat) ?? latest;
|
|
79
|
+
const inputNeeded = mostRecent(
|
|
80
|
+
state.chats,
|
|
81
|
+
(chat) => (chat.status & SessionStatus.InputNeeded) === SessionStatus.InputNeeded,
|
|
82
|
+
);
|
|
83
|
+
const error = mostRecent(state.chats, (chat) => (chat.status & SessionStatus.Error) === SessionStatus.Error);
|
|
84
|
+
if (inputNeeded) {
|
|
85
|
+
driver = inputNeeded;
|
|
86
|
+
} else if (error) {
|
|
87
|
+
driver = error;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const metadataBits = state.status & ~STATUS_ACTIVITY_MASK;
|
|
91
|
+
const activityBits = driver.status & STATUS_ACTIVITY_MASK;
|
|
92
|
+
return {
|
|
93
|
+
status: (metadataBits | activityBits) as SessionStatus,
|
|
94
|
+
...(driver.activity !== undefined ? { activity: driver.activity } : {}),
|
|
95
|
+
modifiedAt: latest.modifiedAt,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function sessionSummaryOf(
|
|
100
|
+
resource: URI,
|
|
101
|
+
createdAt: string,
|
|
102
|
+
state: SessionState,
|
|
103
|
+
meta?: Record<string, unknown>,
|
|
104
|
+
): SessionSummary {
|
|
105
|
+
const aggregate = aggregateSessionChats(state);
|
|
106
|
+
return {
|
|
107
|
+
resource,
|
|
108
|
+
provider: state.provider,
|
|
109
|
+
title: state.title,
|
|
110
|
+
status: aggregate.status,
|
|
111
|
+
// Root summary activity is deliberately omitted for AHP 0.9: its partial
|
|
112
|
+
// update shape cannot distinguish "clear" from "unchanged" on JSON wire.
|
|
113
|
+
createdAt,
|
|
114
|
+
modifiedAt: aggregate.modifiedAt ?? createdAt,
|
|
115
|
+
...(state.workingDirectories ? { workingDirectories: state.workingDirectories } : {}),
|
|
116
|
+
...(meta ? { _meta: meta } : {}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TerminalInfo, TerminalState, URI } from "@microsoft/agent-host-protocol";
|
|
2
|
+
|
|
3
|
+
/** Root catalogue projection; terminal process handles never enter protocol state. */
|
|
4
|
+
export function terminalInfo(resource: URI, state: TerminalState): TerminalInfo {
|
|
5
|
+
return {
|
|
6
|
+
resource,
|
|
7
|
+
title: state.title,
|
|
8
|
+
claim: state.claim,
|
|
9
|
+
lifecycle: state.lifecycle,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Channel URI scheme helpers.
|
|
3
|
+
*
|
|
4
|
+
* Channels are the routing key for the whole protocol: every command and every
|
|
5
|
+
* notification carries `params.channel`, so `(method, channel)` is enough to
|
|
6
|
+
* dispatch a message.
|
|
7
|
+
*
|
|
8
|
+
* @see https://microsoft.github.io/agent-host-protocol/specification/subscriptions
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { URI } from "@microsoft/agent-host-protocol";
|
|
12
|
+
|
|
13
|
+
export const ROOT_CHANNEL = "ahp-root://";
|
|
14
|
+
|
|
15
|
+
const SESSION_SCHEME = "ahp-session:";
|
|
16
|
+
const CHAT_SCHEME = "ahp-chat:";
|
|
17
|
+
const TERMINAL_SCHEME = "ahp-terminal:";
|
|
18
|
+
const CHANGESET_SCHEME = "ahp-changeset:";
|
|
19
|
+
export const RESOURCE_WATCH_SCHEME = "ahp-resource-watch:";
|
|
20
|
+
|
|
21
|
+
export type ChannelKind = "root" | "session" | "chat" | "terminal" | "changeset" | "resourceWatch";
|
|
22
|
+
|
|
23
|
+
/** Whether an action namespace belongs to the reducer behind a channel. */
|
|
24
|
+
export function actionBelongsToChannel(type: unknown, kind: ChannelKind): boolean {
|
|
25
|
+
return typeof type === "string" && type.startsWith(`${kind}/`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isRootChannel(uri: URI): boolean {
|
|
29
|
+
return uri === ROOT_CHANNEL;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isSessionChannel(uri: URI): boolean {
|
|
33
|
+
return uri.startsWith(`${SESSION_SCHEME}/`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isChatChannel(uri: URI): boolean {
|
|
37
|
+
return uri.startsWith(`${CHAT_SCHEME}/`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Classifies a channel URI, or `undefined` for a scheme this host does not
|
|
42
|
+
* serve.
|
|
43
|
+
*
|
|
44
|
+
* Deliberately strict: it drives `subscribe`, where guessing wrong would create
|
|
45
|
+
* a channel with the wrong reducer. A command that creates a resource can
|
|
46
|
+
* register its client-chosen URI with an explicit kind instead.
|
|
47
|
+
*/
|
|
48
|
+
export function channelKind(uri: URI): ChannelKind | undefined {
|
|
49
|
+
if (isRootChannel(uri)) {
|
|
50
|
+
return "root";
|
|
51
|
+
}
|
|
52
|
+
if (isSessionChannel(uri)) {
|
|
53
|
+
return "session";
|
|
54
|
+
}
|
|
55
|
+
if (isChatChannel(uri)) {
|
|
56
|
+
return "chat";
|
|
57
|
+
}
|
|
58
|
+
if (uri.startsWith(`${TERMINAL_SCHEME}/`)) {
|
|
59
|
+
return "terminal";
|
|
60
|
+
}
|
|
61
|
+
if (uri.startsWith(`${CHANGESET_SCHEME}/`)) {
|
|
62
|
+
return "changeset";
|
|
63
|
+
}
|
|
64
|
+
if (uri.startsWith(`${RESOURCE_WATCH_SCHEME}/`)) {
|
|
65
|
+
return "resourceWatch";
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface SingleSegmentChannel {
|
|
71
|
+
readonly scheme: string;
|
|
72
|
+
readonly id: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseSingleSegmentChannel(uri: URI): SingleSegmentChannel | undefined {
|
|
76
|
+
const match = /^([a-zA-Z][a-zA-Z0-9+.-]*):\/([^/?#]+)$/.exec(uri);
|
|
77
|
+
const scheme = match?.[1];
|
|
78
|
+
const id = match?.[2];
|
|
79
|
+
return scheme && id ? { scheme: scheme.toLowerCase(), id } : undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Identifies a session from its canonical scheme or explicit provider aliases
|
|
84
|
+
* given as bare scheme names. Shape alone never makes an unknown URI a session.
|
|
85
|
+
*/
|
|
86
|
+
export function sessionIdFromUri(uri: URI, providerAliases: readonly string[] = []): string | undefined {
|
|
87
|
+
const parsed = parseSingleSegmentChannel(uri);
|
|
88
|
+
if (!parsed) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const canonical = SESSION_SCHEME.slice(0, -1);
|
|
92
|
+
return parsed.scheme === canonical || providerAliases.some((alias) => parsed.scheme === alias.toLowerCase())
|
|
93
|
+
? parsed.id
|
|
94
|
+
: undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function sessionUri(sessionId: string): URI {
|
|
98
|
+
return `${SESSION_SCHEME}/${sessionId}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** `ahp-chat:/<id>` → `<id>`. Returns `undefined` when the URI is not a chat. */
|
|
102
|
+
export function chatIdFromUri(uri: URI): string | undefined {
|
|
103
|
+
return isChatChannel(uri) ? uri.slice(`${CHAT_SCHEME}/`.length) || undefined : undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function chatUri(chatId: string): URI {
|
|
107
|
+
return `${CHAT_SCHEME}/${chatId}`;
|
|
108
|
+
}
|