@akohn/dashql 0.0.2-dev.17 → 0.0.19-dev.247

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 CHANGED
@@ -112,7 +112,8 @@ TypeScript, see `docs/design/dashql_runtime.md`. Only the package on npm is buil
112
112
 
113
113
  - `bazel build //packages/dashql:bundle` bundles the modules with their declarations into `dist/` (`npm/build.mjs`).
114
114
  - `bazel build //packages/dashql:npm_package` assembles the npm directory with `npm/package.json`.
115
- - `bazel build //packages/dashql:npm_tarball` creates the publishable archive.
115
+ - `bazel build //packages/dashql:npm_tarball` creates the publishable archive. It is published together with the
116
+ command line tool, the commands are in the top-level `README.md`.
116
117
  - `bazel test //packages/dashql:node_smoke_test` checks the bindings and a runtime of the assembled package in Node.
117
118
  - `bazel test //packages/dashql:tarball_consumer_test` validates the actual archive as an external consumer.
118
119
  - `bazel test //packages/dashql:test_core_node` runs the colocated tests of the bindings in Node.
@@ -1,4 +1,4 @@
1
- import type { AgentSessionLoginAnswer, AgentSessionOAuth, AgentSessionState } from './agent_session_state.js';
1
+ import type { AgentSessionState } from './agent_session_state.js';
2
2
  import type { SessionConnections } from './workbook_journal.js';
3
3
  export interface AgentSessionTarget {
4
4
  endpoint: string;
@@ -6,7 +6,22 @@ export interface AgentSessionTarget {
6
6
  }
7
7
  export declare function parseAgentSessionTarget(endpoint: unknown, token: unknown): AgentSessionTarget | null;
8
8
  export declare function agentSessionStreamUrl(target: AgentSessionTarget): string;
9
- export declare function followAgentSession(target: AgentSessionTarget, onState: (state: AgentSessionState) => void, signal: AbortSignal): Promise<void>;
9
+ export interface AgentSessionRunRequest {
10
+ workbookId: string;
11
+ label: string;
12
+ scripts: string[];
13
+ refresh: boolean;
14
+ rows: number;
15
+ }
16
+ export type AgentSessionRunMessage = ({
17
+ type: 'run';
18
+ runId: string;
19
+ } & AgentSessionRunRequest) | {
20
+ type: 'stop';
21
+ runId: string;
22
+ };
23
+ export declare const AGENT_SESSION_RUN_HEADER = "X-DashQL-Run";
24
+ export declare function followAgentSession(target: AgentSessionTarget, onState: (state: AgentSessionState) => void, signal: AbortSignal, onRun?: (message: AgentSessionRunMessage) => void): Promise<void>;
10
25
  export interface AgentSessionWorkbookFile {
11
26
  path: string;
12
27
  content: string | Uint8Array<ArrayBuffer> | Blob;
@@ -15,7 +30,12 @@ export declare function writeAgentSessionWorkbook(target: AgentSessionTarget, wo
15
30
  export declare function writeAgentSessionWorkbookFile(target: AgentSessionTarget, workbookId: string, file: AgentSessionWorkbookFile, signal?: AbortSignal): Promise<void>;
16
31
  export declare function deleteAgentSessionWorkbookFile(target: AgentSessionTarget, workbookId: string, path: string, signal?: AbortSignal): Promise<void>;
17
32
  export declare function readAgentSessionWorkbookText(target: AgentSessionTarget, workbookId: string, path: string, signal?: AbortSignal): Promise<string>;
18
- export declare function readAgentSessionResult(target: AgentSessionTarget, key: string, signal?: AbortSignal): Promise<Blob>;
19
33
  export declare function readWorkbookFolderFiles(folder: FileSystemDirectoryHandle): Promise<AgentSessionWorkbookFile[] | null>;
20
- export declare function writeAgentSessionConnections(target: AgentSessionTarget, connections: SessionConnections, oauth?: AgentSessionOAuth | null, signal?: AbortSignal): Promise<void>;
21
- export declare function answerAgentSessionLogin(target: AgentSessionTarget, connectionId: string, answer: AgentSessionLoginAnswer, signal?: AbortSignal): Promise<void>;
34
+ export declare function writeAgentSessionConnections(target: AgentSessionTarget, connections: SessionConnections, signal?: AbortSignal): Promise<void>;
35
+ export declare function startAgentSessionRun(target: AgentSessionTarget, request: AgentSessionRunRequest): Promise<{
36
+ runId: string;
37
+ events: ReadableStream<Uint8Array>;
38
+ }>;
39
+ export declare function stopAgentSessionRun(target: AgentSessionTarget, runId: string): Promise<void>;
40
+ export declare function postAgentSessionRunEvent(target: AgentSessionTarget, runId: string, event: unknown): Promise<boolean>;
41
+ export declare function writeAgentSessionResult(target: AgentSessionTarget, key: string, data: Blob): Promise<string>;
@@ -25,7 +25,8 @@ function agentSessionStreamUrl(target) {
25
25
  url.searchParams.set("token", target.token);
26
26
  return url.toString();
27
27
  }
28
- async function followAgentSession(target, onState, signal) {
28
+ var AGENT_SESSION_RUN_HEADER = "X-DashQL-Run";
29
+ async function followAgentSession(target, onState, signal, onRun) {
29
30
  const response = await fetchAgentSession(agentSessionStreamUrl(target), {
30
31
  signal,
31
32
  headers: { Accept: "text/event-stream" }
@@ -41,8 +42,11 @@ async function followAgentSession(target, onState, signal) {
41
42
  while ((end = buffered.indexOf("\n\n")) >= 0) {
42
43
  const block = buffered.slice(0, end);
43
44
  buffered = buffered.slice(end + 2);
44
- const data = block.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
45
- if (data.length > 0) onState(JSON.parse(data));
45
+ const lines = block.split("\n");
46
+ const data = lines.filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
47
+ if (data.length === 0) continue;
48
+ if (lines.some((line) => line === "event: run")) onRun?.(JSON.parse(data));
49
+ else onState(JSON.parse(data));
46
50
  }
47
51
  }
48
52
  }
@@ -85,12 +89,6 @@ async function readAgentSessionWorkbookText(target, workbookId, path, signal) {
85
89
  await expectOk(response, `the read of ${path}`);
86
90
  return await response.text();
87
91
  }
88
- async function readAgentSessionResult(target, key, signal) {
89
- const url = new URL(`/v1/results/${encodeURIComponent(target.token)}/${encodeURIComponent(key)}`, target.endpoint);
90
- const response = await fetchAgentSession(url, { signal });
91
- if (!response.ok) throw new Error(`the session answered the result ${key} with ${response.status}`);
92
- return await response.blob();
93
- }
94
92
  async function readWorkbookFolderFiles(folder) {
95
93
  let workbook;
96
94
  try {
@@ -119,24 +117,61 @@ async function readWorkbookFolderFiles(folder) {
119
117
  content: workbook
120
118
  }, ...files];
121
119
  }
122
- async function writeAgentSessionConnections(target, connections, oauth = null, signal) {
120
+ async function writeAgentSessionConnections(target, connections, signal) {
123
121
  const url = new URL(`/v1/connections/${encodeURIComponent(target.token)}`, target.endpoint);
124
- const body = oauth == null ? connections : {
125
- ...connections,
126
- oauth
127
- };
128
122
  await expectOk(await fetchAgentSession(url, {
129
123
  method: "PUT",
130
- body: JSON.stringify(body),
124
+ body: JSON.stringify(connections),
131
125
  signal
132
126
  }), "the connections of the agent");
133
127
  }
134
- async function answerAgentSessionLogin(target, connectionId, answer, signal) {
135
- const url = new URL(`/v1/login/${encodeURIComponent(target.token)}/${encodeURIComponent(connectionId)}`, target.endpoint);
136
- await expectOk(await fetchAgentSession(url, {
128
+ function runUrl(target, runId) {
129
+ const parts = [
130
+ "v1",
131
+ "runs",
132
+ target.token,
133
+ ...runId == null ? [] : [runId]
134
+ ];
135
+ return new URL(`/${parts.map(encodeURIComponent).join("/")}`, target.endpoint);
136
+ }
137
+ async function startAgentSessionRun(target, request) {
138
+ const response = await fetchAgentSession(runUrl(target), {
137
139
  method: "POST",
138
- body: JSON.stringify(answer),
139
- signal
140
- }), "the login of the agent");
140
+ body: JSON.stringify(request)
141
+ });
142
+ const runId = response.headers.get(AGENT_SESSION_RUN_HEADER);
143
+ if (!response.ok || runId == null || response.body == null) {
144
+ let error = `the session answered the run with ${response.status}`;
145
+ try {
146
+ const answer = await response.json();
147
+ if (typeof answer?.error === "string") error = answer.error;
148
+ } catch {}
149
+ throw new Error(error);
150
+ }
151
+ return {
152
+ runId,
153
+ events: response.body
154
+ };
155
+ }
156
+ async function stopAgentSessionRun(target, runId) {
157
+ await expectOk(await fetchAgentSession(runUrl(target, runId), { method: "DELETE" }), "the stop of the run");
158
+ }
159
+ async function postAgentSessionRunEvent(target, runId, event) {
160
+ const response = await fetchAgentSession(runUrl(target, runId), {
161
+ method: "POST",
162
+ body: JSON.stringify(event)
163
+ });
164
+ if (response.status === 404) return false;
165
+ await expectOk(response, "the event of the run");
166
+ return true;
167
+ }
168
+ async function writeAgentSessionResult(target, key, data) {
169
+ const url = new URL(`/v1/results/${encodeURIComponent(target.token)}/${encodeURIComponent(key)}`, target.endpoint);
170
+ const response = await fetchAgentSession(url, {
171
+ method: "PUT",
172
+ body: data
173
+ });
174
+ await expectOk(response, `the result ${key}`);
175
+ return (await response.json()).file;
141
176
  }
142
- export { agentSessionStreamUrl, answerAgentSessionLogin, deleteAgentSessionWorkbookFile, followAgentSession, parseAgentSessionTarget, readAgentSessionResult, readAgentSessionWorkbookText, readWorkbookFolderFiles, writeAgentSessionConnections, writeAgentSessionWorkbook, writeAgentSessionWorkbookFile };
177
+ export { AGENT_SESSION_RUN_HEADER, agentSessionStreamUrl, deleteAgentSessionWorkbookFile, followAgentSession, parseAgentSessionTarget, postAgentSessionRunEvent, readAgentSessionWorkbookText, readWorkbookFolderFiles, startAgentSessionRun, stopAgentSessionRun, writeAgentSessionConnections, writeAgentSessionResult, writeAgentSessionWorkbook, writeAgentSessionWorkbookFile };
@@ -1,7 +1,6 @@
1
- import type { OAuthRedirectData } from '../oauth_types.js';
2
1
  import type { LogRecord } from '../logger/log_buffer.js';
3
2
  import { type JournalFile, type SessionWorkbook } from './workbook_journal.js';
4
- export type AgentSessionEventName = 'SessionStart' | 'UserPromptSubmit' | 'PostToolUse' | 'Stop' | 'SessionEnd';
3
+ export type AgentSessionEventName = 'SessionStart' | 'UserPromptSubmit' | 'PreToolUse' | 'PostToolUse' | 'Stop' | 'SessionEnd';
5
4
  export interface AgentSessionEvent {
6
5
  event: AgentSessionEventName;
7
6
  harness: string;
@@ -9,9 +8,17 @@ export interface AgentSessionEvent {
9
8
  prompt?: string | null;
10
9
  status?: string | null;
11
10
  files?: string[];
11
+ toolUseId?: string | null;
12
12
  message?: string | null;
13
13
  error?: string | null;
14
14
  }
15
+ export interface AgentSessionStep {
16
+ label: string;
17
+ toolUseId: string | null;
18
+ startedAt: number;
19
+ endedAt: number | null;
20
+ failed: boolean;
21
+ }
15
22
  export interface AgentSessionTurn {
16
23
  turnId: number;
17
24
  question: string;
@@ -19,30 +26,8 @@ export interface AgentSessionTurn {
19
26
  error: string | null;
20
27
  note: string | null;
21
28
  files: string[];
29
+ steps: AgentSessionStep[];
22
30
  }
23
- export type AgentSessionLoginKind = 'loopback' | 'redirect' | 'credentials';
24
- export interface AgentSessionLogin {
25
- connectionId: string;
26
- name: string;
27
- kind: AgentSessionLoginKind;
28
- url: string;
29
- flowId?: string;
30
- }
31
- export interface AgentSessionOAuth {
32
- redirect: string;
33
- redirectVariant: 'WEB_OPENER_FLOW' | 'NATIVE_LINK_FLOW';
34
- redirectCallbackUrl: string | null;
35
- flavor: string | null;
36
- }
37
- export declare function parseAgentSessionOAuth(text: string): AgentSessionOAuth | null;
38
- export type AgentSessionLoginAnswer = {
39
- oauthRedirect: OAuthRedirectData;
40
- } | {
41
- basic: {
42
- username: string;
43
- secret: string;
44
- };
45
- };
46
31
  export interface AgentSessionState {
47
32
  harness: string;
48
33
  folder: string;
@@ -50,11 +35,11 @@ export interface AgentSessionState {
50
35
  turns: AgentSessionTurn[];
51
36
  workbooks: SessionWorkbook[];
52
37
  log: LogRecord[];
53
- logins: AgentSessionLogin[];
54
38
  version: number;
55
39
  }
56
40
  export declare const MAX_AGENT_SESSION_TURNS = 50;
41
+ export declare const MAX_AGENT_SESSION_STEPS = 30;
57
42
  export declare const MAX_AGENT_SESSION_LOG = 100;
58
43
  export declare function createAgentSessionState(harness: string, folder: string): AgentSessionState;
59
- export declare function reduceAgentSessionEvent(state: AgentSessionState, event: AgentSessionEvent): AgentSessionState;
44
+ export declare function reduceAgentSessionEvent(state: AgentSessionState, event: AgentSessionEvent, now?: number): AgentSessionState;
60
45
  export declare function reduceAgentSessionJournal(state: AgentSessionState, record: LogRecord, files?: JournalFile[]): AgentSessionState;
@@ -1,22 +1,7 @@
1
1
  import { isJournalMarker, reduceJournalMarker } from "./workbook_journal.js";
2
2
  // dist/.stage/src/platform/session/agent_session_state.ts
3
- function parseAgentSessionOAuth(text) {
4
- try {
5
- const oauth = JSON.parse(text)?.oauth;
6
- if (oauth == null || typeof oauth.redirect !== "string" || oauth.redirect.length === 0) return null;
7
- if (oauth.redirectVariant !== "WEB_OPENER_FLOW" && oauth.redirectVariant !== "NATIVE_LINK_FLOW") return null;
8
- const optional = (value) => typeof value === "string" && value.length > 0 ? value : null;
9
- return {
10
- redirect: oauth.redirect,
11
- redirectVariant: oauth.redirectVariant,
12
- redirectCallbackUrl: optional(oauth.redirectCallbackUrl),
13
- flavor: optional(oauth.flavor)
14
- };
15
- } catch {
16
- return null;
17
- }
18
- }
19
3
  var MAX_AGENT_SESSION_TURNS = 50;
4
+ var MAX_AGENT_SESSION_STEPS = 30;
20
5
  var MAX_AGENT_SESSION_LOG = 100;
21
6
  function createAgentSessionState(harness, folder) {
22
7
  return {
@@ -26,7 +11,6 @@ function createAgentSessionState(harness, folder) {
26
11
  turns: [],
27
12
  workbooks: [],
28
13
  log: [],
29
- logins: [],
30
14
  version: 0
31
15
  };
32
16
  }
@@ -37,10 +21,24 @@ function newTurn(state, question) {
37
21
  status: "working",
38
22
  error: null,
39
23
  note: null,
40
- files: []
24
+ files: [],
25
+ steps: []
41
26
  };
42
27
  }
43
- function reduceAgentSessionEvent(state, event) {
28
+ function endSteps(steps, now) {
29
+ return steps.map((step) => step.endedAt == null ? {
30
+ ...step,
31
+ endedAt: now
32
+ } : step);
33
+ }
34
+ function openStepOf(steps, toolUseId, label) {
35
+ if (toolUseId != null) {
36
+ const index = steps.findIndex((step) => step.endedAt == null && step.toolUseId === toolUseId);
37
+ if (index >= 0) return index;
38
+ }
39
+ return steps.findIndex((step) => step.endedAt == null && (step.toolUseId == null || toolUseId == null) && step.label === label);
40
+ }
41
+ function reduceAgentSessionEvent(state, event, now = Date.now()) {
44
42
  let turns = state.turns;
45
43
  const last = turns[turns.length - 1];
46
44
  const open = last != null && last.status != null ? last : null;
@@ -49,14 +47,42 @@ function reduceAgentSessionEvent(state, event) {
49
47
  case "UserPromptSubmit":
50
48
  turns = [...turns, newTurn(state, event.prompt ?? "")];
51
49
  break;
50
+ case "PreToolUse":
52
51
  case "PostToolUse": {
53
52
  const turn = open ?? newTurn(state, "");
54
53
  const files = [...turn.files];
55
54
  for (const file of event.files ?? []) if (!files.includes(file)) files.push(file);
55
+ const label = event.status ?? "tool";
56
+ const toolUseId = event.toolUseId ?? null;
57
+ const steps = [...turn.steps];
58
+ if (event.event === "PreToolUse") steps.push({
59
+ label,
60
+ toolUseId,
61
+ startedAt: now,
62
+ endedAt: null,
63
+ failed: false
64
+ });
65
+ else {
66
+ const failed = event.error != null;
67
+ const index = openStepOf(steps, toolUseId, label);
68
+ if (index >= 0) steps[index] = {
69
+ ...steps[index],
70
+ endedAt: now,
71
+ failed
72
+ };
73
+ else steps.push({
74
+ label,
75
+ toolUseId,
76
+ startedAt: now,
77
+ endedAt: now,
78
+ failed
79
+ });
80
+ }
56
81
  const next = {
57
82
  ...turn,
58
83
  status: event.status ?? turn.status,
59
- files
84
+ files,
85
+ steps: steps.slice(-30)
60
86
  };
61
87
  turns = open != null ? [...turns.slice(0, -1), next] : [...turns, next];
62
88
  break;
@@ -66,12 +92,14 @@ function reduceAgentSessionEvent(state, event) {
66
92
  ...open,
67
93
  status: null,
68
94
  note: event.message ?? null,
69
- error: event.error ?? null
95
+ error: event.error ?? null,
96
+ steps: endSteps(open.steps, now)
70
97
  }];
71
98
  break;
72
99
  case "SessionEnd": if (open != null) turns = [...turns.slice(0, -1), {
73
100
  ...open,
74
- status: null
101
+ status: null,
102
+ steps: endSteps(open.steps, now)
75
103
  }];
76
104
  }
77
105
  return {
@@ -81,7 +109,6 @@ function reduceAgentSessionEvent(state, event) {
81
109
  turns: turns.slice(-50),
82
110
  workbooks: state.workbooks,
83
111
  log: state.log,
84
- logins: state.logins,
85
112
  version: state.version + 1
86
113
  };
87
114
  }
@@ -97,4 +124,4 @@ function reduceAgentSessionJournal(state, record, files = []) {
97
124
  version: state.version + 1
98
125
  };
99
126
  }
100
- export { MAX_AGENT_SESSION_LOG, MAX_AGENT_SESSION_TURNS, createAgentSessionState, parseAgentSessionOAuth, reduceAgentSessionEvent, reduceAgentSessionJournal };
127
+ export { MAX_AGENT_SESSION_LOG, MAX_AGENT_SESSION_STEPS, MAX_AGENT_SESSION_TURNS, createAgentSessionState, reduceAgentSessionEvent, reduceAgentSessionJournal };
@@ -18,46 +18,20 @@ export declare function workbookCopyPath(workbookId: string): string;
18
18
  export declare function sessionJournalPath(session: string | null): string[];
19
19
  export declare const JOURNAL_TARGET = "workbook_journal";
20
20
  export declare const JOURNAL_FILES = "Workbook files";
21
- export declare const JOURNAL_SCRIPT_STARTED = "Script started";
22
- export declare const JOURNAL_SCRIPT_PROGRESS = "Script progress";
23
- export declare const JOURNAL_SCRIPT_RESULT = "Script result";
24
21
  export declare const JOURNAL_KEYS: {
25
22
  readonly workbook: "workbook";
26
23
  readonly workbookId: "workbook_id";
27
24
  readonly files: "files";
28
- readonly script: "script";
29
- readonly phase: "phase";
30
- readonly percent: "percent";
31
- readonly rows: "rows";
32
- readonly key: "key";
33
- readonly resultRows: "result_rows";
34
- readonly error: "error";
35
- readonly cached: "cached";
36
25
  };
37
26
  export interface JournalFile {
38
27
  path: string;
39
28
  size: number;
40
29
  mtimeMs: number;
41
30
  }
42
- export interface JournalResult {
43
- script: string;
44
- key: string | null;
45
- rows: number | null;
46
- error: string | null;
47
- cached: boolean;
48
- }
49
- export interface JournalRunningScript {
50
- script: string;
51
- phase: string | null;
52
- percent: number | null;
53
- rows: number | null;
54
- }
55
31
  export interface SessionWorkbook {
56
32
  path: string;
57
33
  workbookId: string;
58
34
  files: JournalFile[];
59
- results: JournalResult[];
60
- running: JournalRunningScript | null;
61
35
  }
62
36
  export interface JournalWorkbookRef {
63
37
  workbook: string;
@@ -68,9 +42,6 @@ export interface JournalMarker {
68
42
  keyValues: LogKeyValues;
69
43
  }
70
44
  export declare function journalFilesMarker(ref: JournalWorkbookRef, files: readonly string[]): JournalMarker;
71
- export declare function journalScriptStartedMarker(ref: JournalWorkbookRef, script: string): JournalMarker;
72
- export declare function journalScriptProgressMarker(ref: JournalWorkbookRef, running: JournalRunningScript): JournalMarker;
73
- export declare function journalScriptResultMarker(ref: JournalWorkbookRef, result: JournalResult): JournalMarker;
74
45
  export declare function parseJournalLines(text: string): LogRecord[];
75
46
  export declare function isJournalMarker(record: LogRecord): boolean;
76
47
  export declare function journalFilePaths(record: LogRecord): string[] | null;
@@ -34,21 +34,10 @@ function sessionJournalPath(session) {
34
34
  }
35
35
  var JOURNAL_TARGET = "workbook_journal";
36
36
  var JOURNAL_FILES = "Workbook files";
37
- var JOURNAL_SCRIPT_STARTED = "Script started";
38
- var JOURNAL_SCRIPT_PROGRESS = "Script progress";
39
- var JOURNAL_SCRIPT_RESULT = "Script result";
40
37
  var JOURNAL_KEYS = {
41
38
  workbook: "workbook",
42
39
  workbookId: "workbook_id",
43
- files: "files",
44
- script: "script",
45
- phase: "phase",
46
- percent: "percent",
47
- rows: "rows",
48
- key: "key",
49
- resultRows: "result_rows",
50
- error: "error",
51
- cached: "cached"
40
+ files: "files"
52
41
  };
53
42
  function markerOf(message, ref, keyValues) {
54
43
  return {
@@ -60,30 +49,9 @@ function markerOf(message, ref, keyValues) {
60
49
  }
61
50
  };
62
51
  }
63
- var textOfNumber = (value) => value == null ? "" : String(value);
64
52
  function journalFilesMarker(ref, files) {
65
53
  return markerOf(JOURNAL_FILES, ref, { [JOURNAL_KEYS.files]: files });
66
54
  }
67
- function journalScriptStartedMarker(ref, script) {
68
- return markerOf(JOURNAL_SCRIPT_STARTED, ref, { [JOURNAL_KEYS.script]: script });
69
- }
70
- function journalScriptProgressMarker(ref, running) {
71
- return markerOf(JOURNAL_SCRIPT_PROGRESS, ref, {
72
- [JOURNAL_KEYS.script]: running.script,
73
- [JOURNAL_KEYS.phase]: running.phase ?? "",
74
- [JOURNAL_KEYS.percent]: textOfNumber(running.percent == null ? null : Math.round(running.percent)),
75
- [JOURNAL_KEYS.rows]: textOfNumber(running.rows)
76
- });
77
- }
78
- function journalScriptResultMarker(ref, result) {
79
- return markerOf(JOURNAL_SCRIPT_RESULT, ref, {
80
- [JOURNAL_KEYS.script]: result.script,
81
- [JOURNAL_KEYS.key]: result.key ?? "",
82
- [JOURNAL_KEYS.resultRows]: textOfNumber(result.rows),
83
- [JOURNAL_KEYS.error]: result.error ?? "",
84
- [JOURNAL_KEYS.cached]: String(result.cached)
85
- });
86
- }
87
55
  function parseJournalLines(text) {
88
56
  const records = [];
89
57
  for (const line of text.split("\n")) {
@@ -98,12 +66,6 @@ function parseJournalLines(text) {
98
66
  function textOf(value) {
99
67
  return typeof value === "string" ? value : null;
100
68
  }
101
- function numberOf(value) {
102
- const text = textOf(value);
103
- if (text == null || text.length === 0) return null;
104
- const number = Number(text);
105
- return Number.isFinite(number) ? number : null;
106
- }
107
69
  function isJournalMarker(record) {
108
70
  return record.target === JOURNAL_TARGET;
109
71
  }
@@ -113,72 +75,20 @@ function journalFilePaths(record) {
113
75
  return Array.isArray(files) ? files.filter((file) => typeof file === "string") : [];
114
76
  }
115
77
  function reduceJournalMarker(workbooks, record, files = []) {
116
- if (!isJournalMarker(record)) return [...workbooks];
78
+ if (!isJournalMarker(record) || record.message !== "Workbook files") return [...workbooks];
117
79
  const path = textOf(record.keyValues[JOURNAL_KEYS.workbook]);
118
80
  const workbookId = textOf(record.keyValues[JOURNAL_KEYS.workbookId]);
119
81
  if (path == null || workbookId == null) return [...workbooks];
120
82
  const index = workbooks.findIndex((workbook) => workbook.path === path);
121
- const previous = index >= 0 ? workbooks[index] : null;
122
- let next = previous != null && previous.workbookId === workbookId ? previous : {
83
+ const next = {
123
84
  path,
124
85
  workbookId,
125
- files: [],
126
- results: [],
127
- running: null
86
+ files
128
87
  };
129
- const script = textOf(record.keyValues[JOURNAL_KEYS.script]);
130
- switch (record.message) {
131
- case JOURNAL_FILES:
132
- next = {
133
- ...next,
134
- files
135
- };
136
- break;
137
- case JOURNAL_SCRIPT_STARTED:
138
- if (script != null) next = {
139
- ...next,
140
- running: {
141
- script,
142
- phase: null,
143
- percent: null,
144
- rows: null
145
- }
146
- };
147
- break;
148
- case JOURNAL_SCRIPT_PROGRESS:
149
- if (script != null) next = {
150
- ...next,
151
- running: {
152
- script,
153
- phase: textOf(record.keyValues[JOURNAL_KEYS.phase]),
154
- percent: numberOf(record.keyValues[JOURNAL_KEYS.percent]),
155
- rows: numberOf(record.keyValues[JOURNAL_KEYS.rows])
156
- }
157
- };
158
- break;
159
- case JOURNAL_SCRIPT_RESULT: {
160
- if (script == null) break;
161
- const error = textOf(record.keyValues[JOURNAL_KEYS.error]);
162
- const result = {
163
- script,
164
- key: textOf(record.keyValues[JOURNAL_KEYS.key]) || null,
165
- rows: numberOf(record.keyValues[JOURNAL_KEYS.resultRows]),
166
- error: error || null,
167
- cached: record.keyValues[JOURNAL_KEYS.cached] === "true"
168
- };
169
- next = {
170
- ...next,
171
- results: [...next.results.filter((other) => other.script !== script), result],
172
- running: next.running?.script === script ? null : next.running
173
- };
174
- break;
175
- }
176
- default: return [...workbooks];
177
- }
178
88
  return index >= 0 ? [
179
89
  ...workbooks.slice(0, index),
180
90
  next,
181
91
  ...workbooks.slice(index + 1)
182
92
  ] : [...workbooks, next];
183
93
  }
184
- export { JOURNAL_FILES, JOURNAL_KEYS, JOURNAL_SCRIPT_PROGRESS, JOURNAL_SCRIPT_RESULT, JOURNAL_SCRIPT_STARTED, JOURNAL_TARGET, SESSION_CONNECTIONS_FILE, SESSION_JOURNAL_FILE, SESSION_JOURNAL_FOLDER, SESSION_JOURNAL_SESSIONS, SESSION_WORKBOOK_COPIES, isJournalMarker, isSessionName, isWorkbookCopyId, journalFilePaths, journalFilesMarker, journalScriptProgressMarker, journalScriptResultMarker, journalScriptStartedMarker, parseJournalLines, parseSessionConnections, reduceJournalMarker, sessionJournalPath, workbookCopyPath };
94
+ export { JOURNAL_FILES, JOURNAL_KEYS, JOURNAL_TARGET, SESSION_CONNECTIONS_FILE, SESSION_JOURNAL_FILE, SESSION_JOURNAL_FOLDER, SESSION_JOURNAL_SESSIONS, SESSION_WORKBOOK_COPIES, isJournalMarker, isSessionName, isWorkbookCopyId, journalFilePaths, journalFilesMarker, parseJournalLines, parseSessionConnections, reduceJournalMarker, sessionJournalPath, workbookCopyPath };
@@ -0,0 +1,66 @@
1
+ import * as arrow from 'apache-arrow';
2
+ export type RunEvent = {
3
+ type: 'run_started';
4
+ workbook: string;
5
+ scripts: string[];
6
+ } | {
7
+ type: 'connection_setup';
8
+ connection: string;
9
+ } | {
10
+ type: 'connection_ready';
11
+ connection: string;
12
+ ms: number;
13
+ } | {
14
+ type: 'connection_failed';
15
+ connection: string;
16
+ error: string;
17
+ } | {
18
+ type: 'script_planned';
19
+ script: string;
20
+ follows: string[];
21
+ } | {
22
+ type: 'script_started';
23
+ script: string;
24
+ } | {
25
+ type: 'script_progress';
26
+ script: string;
27
+ phase: string | null;
28
+ percent: number | null;
29
+ rows: number | null;
30
+ } | {
31
+ type: 'script_done';
32
+ script: string;
33
+ ms: number;
34
+ rows: number | null;
35
+ table: arrow.Table | null;
36
+ cached: boolean;
37
+ key: string | null;
38
+ file: string | null;
39
+ } | {
40
+ type: 'script_failed';
41
+ script: string;
42
+ ms: number;
43
+ error: string;
44
+ statement: string | null;
45
+ } | {
46
+ type: 'script_cancelled';
47
+ script: string;
48
+ ms: number;
49
+ } | {
50
+ type: 'script_not_run';
51
+ script: string;
52
+ reason: string;
53
+ } | {
54
+ type: 'run_failed';
55
+ error: string;
56
+ } | {
57
+ type: 'run_finished';
58
+ ms: number;
59
+ ok: number;
60
+ failed: number;
61
+ cancelled: number;
62
+ notRun: number;
63
+ interrupted: boolean;
64
+ };
65
+ export declare function encodeRunEvent(event: RunEvent, rows: number): unknown;
66
+ export declare function decodeRunEvent(value: unknown): RunEvent | null;
@@ -0,0 +1,38 @@
1
+ import * as arrow from "apache-arrow";
2
+ // dist/.stage/src/platform/workbook/run_events.ts
3
+ function fromBase64(text) {
4
+ const binary = atob(text);
5
+ const bytes = new Uint8Array(binary.length);
6
+ for (let i = 0; i < binary.length; ++i) bytes[i] = binary.charCodeAt(i);
7
+ return bytes;
8
+ }
9
+ function toBase64(bytes) {
10
+ let binary = "";
11
+ const chunk = 32768;
12
+ for (let i = 0; i < bytes.length; i += chunk) binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
13
+ return btoa(binary);
14
+ }
15
+ function encodeRunEvent(event, rows) {
16
+ if (event.type !== "script_done") return event;
17
+ const table = event.table == null ? null : toBase64(arrow.tableToIPC(event.table.slice(0, rows), "stream"));
18
+ return {
19
+ ...event,
20
+ table
21
+ };
22
+ }
23
+ function decodeRunEvent(value) {
24
+ if (value == null || typeof value !== "object" || typeof value.type !== "string") return null;
25
+ const event = value;
26
+ if (event.type !== "script_done") return event;
27
+ let table = null;
28
+ if (typeof event.table === "string") try {
29
+ table = arrow.tableFromIPC(fromBase64(event.table));
30
+ } catch {
31
+ table = null;
32
+ }
33
+ return {
34
+ ...event,
35
+ table
36
+ };
37
+ }
38
+ export { decodeRunEvent, encodeRunEvent };