@jc_stack/ez-agents 0.1.0-beta.47 → 0.1.0-beta.48
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/.env.example +3 -0
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +21 -0
- package/Dockerfile +5 -1
- package/THIRD_PARTY_NOTICES.md +6 -3
- package/bin/ez +7 -0
- package/docker/broker-readiness.mjs +54 -0
- package/docker/broker-update-smoke.mjs +86 -0
- package/docker/run.ts +1 -1
- package/docs/application-channel.md +32 -1
- package/docs/architecture/ai-selection.md +18 -3
- package/docs/docker-runtime.md +29 -0
- package/docs/isolated-plugin-update-readiness.md +66 -0
- package/docs/releasing.md +4 -0
- package/docs/scheduling.md +100 -6
- package/docs/selective-monitoring.md +13 -0
- package/docs/upgrades.md +35 -0
- package/docs/work-preflight.md +9 -0
- package/package.json +1 -1
- package/src/ai.ts +22 -3
- package/src/application-channel.ts +43 -3
- package/src/application-cli.ts +7 -1
- package/src/application-origin.ts +2 -0
- package/src/audio.ts +37 -22
- package/src/client-defaults.ts +3 -2
- package/src/config.ts +9 -0
- package/src/control-state.ts +3 -1
- package/src/delivery-socket-client.d.mts +2 -0
- package/src/delivery-socket-client.mjs +2 -0
- package/src/delivery-socket.ts +24 -1
- package/src/event-sources.ts +2 -2
- package/src/executor.ts +72 -1
- package/src/files.ts +47 -3
- package/src/host-executor.ts +38 -29
- package/src/index.ts +41 -18
- package/src/install-tools.mjs +8 -5
- package/src/menu.ts +4 -2
- package/src/plugin-broker.mjs +47 -4
- package/src/plugins/manager.mjs +11 -4
- package/src/run-evidence.ts +78 -0
- package/src/runs.ts +12 -1
- package/src/schedule-cli.ts +69 -8
- package/src/schedule-preflight.ts +23 -0
- package/src/scheduled-tasks.ts +16 -12
- package/src/scheduler.ts +79 -18
- package/src/scripts.ts +167 -0
- package/src/setup.ts +2 -2
- package/src/task-capability.ts +17 -1
- package/src/task-executor.ts +10 -7
- package/src/task-mcp.ts +20 -8
- package/src/tasks.ts +89 -9
- package/src/updates/runtime.mjs +46 -7
- package/test/ai.test.ts +40 -0
- package/test/application-channel.test.ts +16 -1
- package/test/audio.test.ts +19 -0
- package/test/config.test.ts +8 -0
- package/test/executor.test.ts +37 -0
- package/test/files.test.ts +44 -0
- package/test/host-executor.test.ts +45 -2
- package/test/install-json.test.mjs +21 -0
- package/test/plugin-broker.test.mjs +64 -0
- package/test/plugin-manager.test.mjs +20 -0
- package/test/private-speech-config.test.ts +22 -0
- package/test/run-evidence.test.ts +78 -0
- package/test/schedule-preflight.test.ts +49 -0
- package/test/scheduler-host.test.ts +7 -0
- package/test/scheduler-relay.test.ts +38 -3
- package/test/scheduler.test.ts +1 -1
- package/test/script-schedules.test.ts +219 -0
- package/test/task-native.test.ts +50 -12
- package/test/tasks.test.ts +97 -4
- package/test/updates.test.mjs +45 -9
package/src/scripts.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, writeFile, rename, link, rm, realpath, stat, access } from 'node:fs/promises'
|
|
2
|
+
import { constants as fsConstants } from 'node:fs'
|
|
3
|
+
import { randomUUID, createHash } from 'node:crypto'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { type Owner, sameOwner, validOwner } from './control-state.js'
|
|
6
|
+
import { assertId } from './identity.js'
|
|
7
|
+
|
|
8
|
+
// Registered scripts reference agent-owned code in the workspace. Core keeps
|
|
9
|
+
// only the registration: no second source copy, interpreter or dependency setup.
|
|
10
|
+
export type ScriptRegistration = {
|
|
11
|
+
version: 1; id: string; revision: string; owner: Owner
|
|
12
|
+
entry: string; interpreter: string; args: string[]; sha256: string
|
|
13
|
+
timeoutSeconds: number; registeredAt: string; updatedAt: string
|
|
14
|
+
}
|
|
15
|
+
// Captured on the run when an occurrence is queued; the launcher verifies it.
|
|
16
|
+
export type ScriptRunRef = { id: string; revision?: string; sha256?: string; args: string[] }
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_SCRIPT_TIMEOUT_SECONDS = 900
|
|
19
|
+
export const MAX_SCRIPT_TIMEOUT_SECONDS = 21600
|
|
20
|
+
const SCRIPT_ID = /^[a-z0-9][a-z0-9_-]{0,63}$/
|
|
21
|
+
const COMMAND = /^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$/
|
|
22
|
+
|
|
23
|
+
export const assertScriptId = (id: unknown): string => {
|
|
24
|
+
if (typeof id !== 'string' || !SCRIPT_ID.test(id)) throw new Error('Script ID must be 1-64 lowercase letters, digits, _ or -')
|
|
25
|
+
return id
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const assertScriptArgs = (args: unknown): string[] => {
|
|
29
|
+
if (!Array.isArray(args) || args.length > 64 || args.some(arg => typeof arg !== 'string' || arg.length > 4096 || arg.includes('\0')))
|
|
30
|
+
throw new Error('Script arguments must be at most 64 literal strings')
|
|
31
|
+
return args as string[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// An installed interpreter is a bare command resolved on the executor PATH or an
|
|
35
|
+
// absolute path. Anything with whitespace or shell syntax is a command string.
|
|
36
|
+
export const assertInterpreter = (value: unknown): string => {
|
|
37
|
+
if (typeof value !== 'string' || !(COMMAND.test(value) || (path.isAbsolute(value) && !/[\s\0;&|`$<>()'"\\*?]/.test(value))))
|
|
38
|
+
throw new Error('Interpreter must be an installed command name or absolute path, not a shell command')
|
|
39
|
+
return value
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const assertTimeout = (value: unknown): number => {
|
|
43
|
+
if (!Number.isSafeInteger(value) || (value as number) < 1 || (value as number) > MAX_SCRIPT_TIMEOUT_SECONDS)
|
|
44
|
+
throw new Error(`Script timeout must be 1..${MAX_SCRIPT_TIMEOUT_SECONDS} seconds`)
|
|
45
|
+
return value as number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const validScriptRunRef = (value: unknown): value is ScriptRunRef => {
|
|
49
|
+
const ref = value as ScriptRunRef
|
|
50
|
+
try {
|
|
51
|
+
return Boolean(ref) && assertScriptId(ref.id) === ref.id && assertScriptArgs(ref.args) === ref.args &&
|
|
52
|
+
(ref.revision === undefined || (typeof ref.revision === 'string' && /^[a-zA-Z0-9_-]+$/.test(ref.revision))) &&
|
|
53
|
+
(ref.sha256 === undefined || (typeof ref.sha256 === 'string' && /^[a-f0-9]{64}$/.test(ref.sha256)))
|
|
54
|
+
} catch { return false }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The interpreter must be an installed runtime, not unhashed workspace code.
|
|
58
|
+
export const resolveInterpreter = async (interpreter: string, pathValue = process.env.PATH, workspace?: string): Promise<string> => {
|
|
59
|
+
assertInterpreter(interpreter)
|
|
60
|
+
const candidates = path.isAbsolute(interpreter) ? [interpreter]
|
|
61
|
+
// Relative PATH entries would resolve against a different cwd at spawn time.
|
|
62
|
+
: (pathValue ?? '').split(path.delimiter).filter(directory => path.isAbsolute(directory)).map(directory => path.join(directory, interpreter))
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
let real: string
|
|
65
|
+
try { await access(candidate, fsConstants.X_OK); if (!(await stat(candidate)).isFile()) continue; real = await realpath(candidate) } catch { continue }
|
|
66
|
+
if (workspace && real.startsWith(await realpath(workspace) + path.sep)) throw new Error('Interpreter must be an installed runtime outside the agent workspace')
|
|
67
|
+
return candidate
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Interpreter ${interpreter} is not installed on the executor PATH`)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Resolve a workspace-relative entry point to its real file inside the workspace.
|
|
73
|
+
export const workspaceEntry = async (workspace: string, entry: string): Promise<{ file: string; relative: string }> => {
|
|
74
|
+
if (typeof entry !== 'string' || !entry || entry.includes('\0')) throw new Error('Script entry point is required')
|
|
75
|
+
const root = await realpath(workspace)
|
|
76
|
+
let file: string
|
|
77
|
+
try { file = await realpath(path.resolve(root, entry)) }
|
|
78
|
+
catch { throw new Error('Script entry point does not exist in the agent workspace') }
|
|
79
|
+
if (!file.startsWith(root + path.sep) || !(await stat(file)).isFile()) throw new Error('Script entry point must be a file inside the agent workspace')
|
|
80
|
+
return { file, relative: path.relative(root, file) }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const fileSha256 = async (file: string) => createHash('sha256').update(await readFile(file)).digest('hex')
|
|
84
|
+
|
|
85
|
+
const atomic = async (file: string, value: unknown, exclusive = false) => {
|
|
86
|
+
const tmp = `${file}.${randomUUID()}.tmp`
|
|
87
|
+
try {
|
|
88
|
+
await writeFile(tmp, JSON.stringify(value) + '\n', { mode: 0o600, flag: 'wx' })
|
|
89
|
+
if (exclusive) await link(tmp, file)
|
|
90
|
+
else await rename(tmp, file)
|
|
91
|
+
} finally { await rm(tmp, { force: true }) }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class Scripts {
|
|
95
|
+
private dir: string
|
|
96
|
+
constructor(controlDir: string) { this.dir = path.join(controlDir, 'scripts') }
|
|
97
|
+
|
|
98
|
+
async get(id: string): Promise<ScriptRegistration> {
|
|
99
|
+
const s = JSON.parse(await readFile(path.join(this.dir, assertId(assertScriptId(id)) + '.json'), 'utf8')) as ScriptRegistration
|
|
100
|
+
if (s.version !== 1 || s.id !== id || typeof s.revision !== 'string' || !/^[a-zA-Z0-9_-]+$/.test(s.revision) || !validOwner(s.owner) ||
|
|
101
|
+
typeof s.entry !== 'string' || !s.entry || path.isAbsolute(s.entry) || s.entry.split(/[\\/]/).includes('..') ||
|
|
102
|
+
!/^[a-f0-9]{64}$/.test(s.sha256)) throw new Error('Invalid script registration')
|
|
103
|
+
assertInterpreter(s.interpreter); assertScriptArgs(s.args); assertTimeout(s.timeoutSeconds)
|
|
104
|
+
return s
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async owned(id: string, owner: Owner): Promise<ScriptRegistration> {
|
|
108
|
+
let s: ScriptRegistration
|
|
109
|
+
try { s = await this.get(id) }
|
|
110
|
+
catch (error) {
|
|
111
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') throw new Error(`Script ${id} is not registered`)
|
|
112
|
+
throw error
|
|
113
|
+
}
|
|
114
|
+
if (!sameOwner(s.owner, owner)) throw new Error('Script registration is outside this owner binding')
|
|
115
|
+
return s
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async list(owner: Owner): Promise<ScriptRegistration[]> {
|
|
119
|
+
let names: string[]
|
|
120
|
+
try { names = await readdir(this.dir) }
|
|
121
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []; throw error }
|
|
122
|
+
const result: ScriptRegistration[] = []
|
|
123
|
+
for (const name of names.filter(name => /^[a-z0-9][a-z0-9_-]*\.json$/.test(name)).sort()) {
|
|
124
|
+
try { const s = await this.get(name.slice(0, -5)); if (sameOwner(s.owner, owner)) result.push(s) }
|
|
125
|
+
catch { console.error('Unreadable script registration', name) }
|
|
126
|
+
}
|
|
127
|
+
return result
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Register or explicitly update: hashes the current entry-point bytes.
|
|
131
|
+
async save(input: { id: string; owner: Owner; workspace: string; entry: string; interpreter: string; args: string[]; timeoutSeconds: number; pathValue?: string }, create: boolean): Promise<ScriptRegistration> {
|
|
132
|
+
assertScriptId(input.id)
|
|
133
|
+
if (!validOwner(input.owner)) throw new Error('Script registration requires the paired owner')
|
|
134
|
+
const previous = create ? undefined : await this.owned(input.id, input.owner)
|
|
135
|
+
const { file, relative } = await workspaceEntry(input.workspace, input.entry)
|
|
136
|
+
await resolveInterpreter(assertInterpreter(input.interpreter), input.pathValue, input.workspace)
|
|
137
|
+
const now = new Date().toISOString()
|
|
138
|
+
const s: ScriptRegistration = { version: 1, id: input.id, revision: randomUUID(), owner: input.owner, entry: relative,
|
|
139
|
+
interpreter: input.interpreter, args: assertScriptArgs(input.args), sha256: await fileSha256(file),
|
|
140
|
+
timeoutSeconds: assertTimeout(input.timeoutSeconds), registeredAt: previous?.registeredAt ?? now, updatedAt: now }
|
|
141
|
+
await mkdir(this.dir, { recursive: true, mode: 0o700 })
|
|
142
|
+
try { await atomic(path.join(this.dir, s.id + '.json'), s, create) }
|
|
143
|
+
catch (error) {
|
|
144
|
+
if (create && (error as NodeJS.ErrnoException).code === 'EEXIST') throw new Error('Script is already registered; use update')
|
|
145
|
+
throw error
|
|
146
|
+
}
|
|
147
|
+
return s
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async remove(id: string, owner: Owner) {
|
|
151
|
+
await this.owned(id, owner)
|
|
152
|
+
await rm(path.join(this.dir, assertScriptId(id) + '.json'))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Executor-side check, immediately before spawn: the queued registration
|
|
156
|
+
// revision must still be current and the entry-point bytes must match it.
|
|
157
|
+
async verifiedInvocation(ref: ScriptRunRef, workspace: string, pathValue?: string) {
|
|
158
|
+
if (!validScriptRunRef(ref) || !ref.revision || !ref.sha256) throw new Error(`Script ${ref?.id ?? ''} was not registered when queued`)
|
|
159
|
+
let s: ScriptRegistration
|
|
160
|
+
try { s = await this.get(ref.id) } catch { throw new Error(`Script ${ref.id} registration is unavailable`) }
|
|
161
|
+
if (s.revision !== ref.revision || s.sha256 !== ref.sha256) throw new Error(`Script ${ref.id} registration changed after this run was queued`)
|
|
162
|
+
const { file } = await workspaceEntry(workspace, s.entry)
|
|
163
|
+
if (await fileSha256(file) !== s.sha256) throw new Error(`Script ${ref.id} entry point changed since registration; inspect it and run an explicit registration update`)
|
|
164
|
+
const command = await resolveInterpreter(s.interpreter, pathValue, workspace)
|
|
165
|
+
return { command, args: [file, ...s.args, ...ref.args], registration: s }
|
|
166
|
+
}
|
|
167
|
+
}
|
package/src/setup.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { discoverDefaults } from './client-defaults.js'
|
|
|
9
9
|
import { chatPreset } from './ai.js'
|
|
10
10
|
import { ControlStore } from './control-state.js'
|
|
11
11
|
import { loadControlConfig } from './config.js'
|
|
12
|
-
import { EXECUTOR_REGISTRY, resolveExecutor, executorKey } from './executor.js'
|
|
12
|
+
import { EXECUTOR_REGISTRY, resolveExecutor, executorKey, claudeConfigHome } from './executor.js'
|
|
13
13
|
import { desktopCodexPath } from './desktop-bridge.js'
|
|
14
14
|
|
|
15
15
|
export const findExecutableInPath = async (
|
|
@@ -152,7 +152,7 @@ export const runCli = async (): Promise<void> => {
|
|
|
152
152
|
const config = loadControlConfig()
|
|
153
153
|
await new ControlStore(config.controlDir, config.pairingTtlMs).syncClientPresets(
|
|
154
154
|
chatPreset(await readActiveExecutor(envFilePath)), await discoverDefaults(workspace,
|
|
155
|
-
{ codexHome: path.join(config.controlDir, 'cli', 'codex') }))
|
|
155
|
+
{ codexHome: path.join(config.controlDir, 'cli', 'codex'), claudeHome: claudeConfigHome(config.controlDir) }))
|
|
156
156
|
console.log(JSON.stringify({ workspace, created }))
|
|
157
157
|
return
|
|
158
158
|
}
|
package/src/task-capability.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { execFile } from 'node:child_process'
|
|
1
|
+
import { execFile, spawn } from 'node:child_process'
|
|
2
2
|
import { promisify } from 'node:util'
|
|
3
3
|
import { readFile } from 'node:fs/promises'
|
|
4
4
|
import path from 'node:path'
|
|
5
|
+
import {fileURLToPath} from 'node:url'
|
|
5
6
|
import { executorEnvironment } from './executor.js'
|
|
6
7
|
import type { TaskCapability } from './tasks.js'
|
|
7
8
|
import {attachmentLimit,stageTaskAttachment,type TaskAttachment} from './task-attachments.js'
|
|
@@ -43,3 +44,18 @@ export async function runTaskCapability(toolsHome: string | undefined, capabilit
|
|
|
43
44
|
if (code !== 0 || !stdout.trim()) throw new Error(`Channel capability failed with exit ${code}`)
|
|
44
45
|
return {output:stdout}
|
|
45
46
|
}
|
|
47
|
+
|
|
48
|
+
export async function launchTaskApplication(toolsHome: string | undefined, launch:{command:string;args:string[]}, input:unknown, runId:string):Promise<unknown> {
|
|
49
|
+
const isolated=!!process.env.EZ_PLUGIN_BROKER_SOCKET
|
|
50
|
+
if(!isolated && (!toolsHome || !path.isAbsolute(toolsHome)))throw Error('Installed tool registry required')
|
|
51
|
+
const config=!isolated ? JSON.parse(await readFile(path.join(toolsHome!,'config.json'),'utf8')) : undefined
|
|
52
|
+
// Fixed owner-configured command; literal private stdin, never a shell or model-selected args.
|
|
53
|
+
return new Promise((resolve,reject)=>{
|
|
54
|
+
const child=spawn(isolated ? process.execPath : path.join(toolsHome!,'bin','ez'),isolated ? [fileURLToPath(new URL('../bin/ez',import.meta.url)),'--task-application'] : [launch.command,...launch.args],{env:{...executorEnvironment(),EZ_RUN_ID:runId},...(config ? {cwd:config.workspace} : {}),stdio:['pipe','pipe','pipe']})
|
|
55
|
+
let output='';const timer=setTimeout(()=>{child.kill();reject(Error('Application launch timed out'))},30000)
|
|
56
|
+
child.stdout.on('data',chunk=>{output+=chunk;if(Buffer.byteLength(output)>16000){child.kill();reject(Error('Application launch output too large'))}})
|
|
57
|
+
child.stderr.resume();child.on('error',reject)
|
|
58
|
+
child.on('close',code=>{clearTimeout(timer);try{if(code!==0)throw Error('Application launch failed');const value=JSON.parse(output);const url=new URL(value.launch?.url);if(url.protocol!=='https:' || url.username || url.password || typeof value.launch?.expiresAt!=='number')throw Error('Invalid application launch receipt');resolve(value.launch)}catch(e){reject(e)}})
|
|
59
|
+
child.stdin.end(JSON.stringify(input))
|
|
60
|
+
})
|
|
61
|
+
}
|
package/src/task-executor.ts
CHANGED
|
@@ -6,12 +6,13 @@ import { fileURLToPath } from 'node:url'
|
|
|
6
6
|
import { spawn, execFile } from 'node:child_process'
|
|
7
7
|
import { promisify } from 'node:util'
|
|
8
8
|
import { readRun } from './delivery-socket.js'
|
|
9
|
+
import {ApplicationBindings} from './application-channel.js'
|
|
9
10
|
import { Tasks } from './tasks.js'
|
|
10
11
|
import { executorEnvironment, executorInvocation, terminateJob, type ExecutorOptions } from './executor.js'
|
|
11
12
|
|
|
12
13
|
// This adapter is deliberately version-pinned: a new native tool default needs
|
|
13
14
|
// a fresh tool-inventory audit before external correspondence can use it.
|
|
14
|
-
export const TASK_CODEX_VERSION = '0.160.
|
|
15
|
+
export const TASK_CODEX_VERSION = '0.160.1'
|
|
15
16
|
export const taskDisabledFeatures = ['apps', 'browser_use', 'computer_use', 'in_app_browser', 'image_generation',
|
|
16
17
|
'memories', 'goals', 'multi_agent', 'multi_agent_v2', 'hooks', 'shell_tool', 'unified_exec', 'code_mode', 'code_mode_host',
|
|
17
18
|
'skill_search', 'skill_mcp_dependency_install', 'tool_suggest', 'workspace_dependencies', 'view_image']
|
|
@@ -23,8 +24,9 @@ export function taskModelCatalog(catalog: { models: Record<string, unknown>[] })
|
|
|
23
24
|
apply_patch_tool_type: null, experimental_supported_tools: [], multi_agent_version: null,
|
|
24
25
|
supports_search_tool: false, use_responses_lite: false })) };
|
|
25
26
|
}
|
|
26
|
-
export function taskArguments(directory: string, broker: string[], prompt: string, toolNames = ['context', 'send', 'note', 'report', 'complete'], selection: {model?:string;effort?:string} = {}) {
|
|
27
|
+
export function taskArguments(directory: string, broker: string[], prompt: string, toolNames = ['context', 'read_attachment', 'send', 'note', 'report', 'complete'], selection: {model?:string;effort?:string} = {}, brokerEnvironment: NodeJS.ProcessEnv = {}) {
|
|
27
28
|
const preset = executionDefaults('codex', selection)
|
|
29
|
+
const brokerEnv={HOME:homedir(),...Object.fromEntries(['EZ_PLUGIN_BROKER_SOCKET','EZ_DELIVERY_SOCKET'].flatMap(key=>brokerEnvironment[key] ? [[key,brokerEnvironment[key]]] : []))}
|
|
28
30
|
return ['exec', ...(preset.model ? ['--model',preset.model] : []), ...(preset.effort ? ['-c',`model_reasoning_effort=${JSON.stringify(preset.effort)}`] : []), '--skip-git-repo-check', '--ignore-user-config', '--ignore-rules', '--ephemeral', '--strict-config', '--json', '-C', directory,
|
|
29
31
|
...taskDisabledFeatures.flatMap(feature => ['--disable', feature]), '--enable', 'skip_host_skill_discovery',
|
|
30
32
|
'-c', `model_catalog_json=${JSON.stringify(join(directory, '..', 'models.json'))}`,
|
|
@@ -33,7 +35,7 @@ export function taskArguments(directory: string, broker: string[], prompt: strin
|
|
|
33
35
|
'-c', `permissions.ez-task.filesystem={":root"="deny",":minimal"="read",${JSON.stringify(directory)}="write"}`,
|
|
34
36
|
'-c', 'permissions.ez-task.network.enabled=false',
|
|
35
37
|
// The trusted broker needs the host's CLI configuration; the model keeps its isolated HOME.
|
|
36
|
-
'-c', `mcp_servers.ez={command=${JSON.stringify(broker[0])},args=${JSON.stringify(broker.slice(1))},env={
|
|
38
|
+
'-c', `mcp_servers.ez={command=${JSON.stringify(broker[0])},args=${JSON.stringify(broker.slice(1))},env={${Object.entries(brokerEnv).map(([key,value])=>`${key}=${JSON.stringify(value)}`).join(',')}},required=true,enabled_tools=${JSON.stringify(toolNames)}}`,
|
|
37
39
|
...toolNames.flatMap(name => ['-c', `mcp_servers.ez.tools.${name}.approval_mode="approve"`]),
|
|
38
40
|
'-'] // Literal input travels on stdin, including slash commands and leading options.
|
|
39
41
|
}
|
|
@@ -41,9 +43,10 @@ export async function startTaskExecutor(options: ExecutorOptions) {
|
|
|
41
43
|
const run = await readRun(options.controlDir, options.runId)
|
|
42
44
|
if (!run || run.status !== 'running') throw new Error('No active task run')
|
|
43
45
|
const task = await new Tasks(options.controlDir).authorize(run, false)
|
|
46
|
+
const applications=(run.application || task.anyConversation) ? [] : (await new ApplicationBindings(options.controlDir).list()).filter(b=>b.taskLaunch && (b.taskLaunch.tasks==="all" || b.taskLaunch.tasks.includes(task.id))).map(b=>b.id)
|
|
44
47
|
const capabilityNames = (task.capabilities ?? []).map(item => `capability_${item.id}`)
|
|
45
|
-
const toolNames = [...(task.anyConversation ? ['context','send'] : ['context','send','note','report','complete']),...capabilityNames]
|
|
46
|
-
if (capabilityNames.length && !options.toolsHome) throw new Error('Channel capabilities require an installed tool registry')
|
|
48
|
+
const toolNames = [...(task.anyConversation ? ['context','read_attachment','send'] : ['context','read_attachment','send','note','report','complete']),...capabilityNames,...(applications.length?["browser_link"]:[])]
|
|
49
|
+
if ((capabilityNames.length || (applications.length && !process.env.EZ_PLUGIN_BROKER_SOCKET)) && !options.toolsHome) throw new Error('Channel capabilities require an installed tool registry')
|
|
47
50
|
const environment = executorEnvironment()
|
|
48
51
|
const versionInvocation = executorInvocation('codex', ['--version'])
|
|
49
52
|
const version = await promisify(execFile)(versionInvocation.command, versionInvocation.args, { env: environment })
|
|
@@ -60,9 +63,9 @@ export async function startTaskExecutor(options: ExecutorOptions) {
|
|
|
60
63
|
: join(homedir(), '.codex', 'auth.json')
|
|
61
64
|
await symlink(auth, join(home, 'auth.json'))
|
|
62
65
|
const broker = [process.execPath, '--import', fileURLToPath(new URL('../node_modules/tsx/dist/loader.mjs', import.meta.url)),
|
|
63
|
-
fileURLToPath(new URL('./task-mcp.ts', import.meta.url)), options.controlDir, options.runId, options.toolsHome ?? '', JSON.stringify(task.capabilities ?? [])]
|
|
66
|
+
fileURLToPath(new URL('./task-mcp.ts', import.meta.url)), options.controlDir, options.runId, options.toolsHome ?? '', JSON.stringify(task.capabilities ?? []),JSON.stringify(applications)]
|
|
64
67
|
const prompt = JSON.stringify({event: run.external ? 'correspondence_received' : 'task_activated', taskId: run.taskId})
|
|
65
|
-
const invocation = executorInvocation('codex', taskArguments(directory, broker, prompt, toolNames, options))
|
|
68
|
+
const invocation = executorInvocation('codex', taskArguments(directory, broker, prompt, toolNames, options, Object.fromEntries(['EZ_PLUGIN_BROKER_SOCKET','EZ_DELIVERY_SOCKET'].flatMap(key=>environment[key] ? [[key,environment[key]]] : []))))
|
|
66
69
|
const child = spawn(invocation.command, invocation.args, {
|
|
67
70
|
cwd: directory, env: { ...environment, HOME: home, CODEX_HOME: home }, stdio: ['pipe', 'pipe', 'pipe'], detached: process.platform !== 'win32',
|
|
68
71
|
})
|
package/src/task-mcp.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import {randomBytes,createHash} from 'node:crypto'
|
|
1
2
|
import { createInterface } from 'node:readline'
|
|
2
3
|
import { taskCall } from './task-rpc.js'
|
|
3
|
-
import { runTaskCapability } from './task-capability.js'
|
|
4
|
+
import { runTaskCapability,launchTaskApplication } from './task-capability.js'
|
|
4
5
|
import type { TaskCapability } from './tasks.js'
|
|
5
|
-
const [controlDir, runId, toolsHome, capabilityJson='[]'] = process.argv.slice(2)
|
|
6
|
+
const [controlDir, runId, toolsHome, capabilityJson='[]', applicationsJson='[]'] = process.argv.slice(2)
|
|
6
7
|
const capabilities = JSON.parse(capabilityJson) as TaskCapability[]
|
|
8
|
+
const applications=JSON.parse(applicationsJson) as string[]
|
|
7
9
|
const descriptions: Record<string, string> = {
|
|
10
|
+
...(applications.length ? {browser_link:`Create a short-lived single-use browser conversation link for this discussion. Applications: ${applications.join(", ")}. Return the exact link as text to this same discussion; the link is a private bearer credential.`} : {}),
|
|
8
11
|
context: 'Read the owner-approved purpose and shareable context, task notes, receipts, and untrusted correspondence.',
|
|
9
|
-
|
|
12
|
+
read_attachment: 'Read a JPEG/PNG/WebP image, an MP4 video as sampled frames (up to 120 seconds), or a TXT/Markdown/PDF document from an incomingId in this run context. Returns the image to native vision, or bounded extracted document text, with file provenance; no owner files are accessible.',
|
|
13
|
+
send: 'Send text, or set voice=true for a spoken reply (at most 1000 characters). Optionally use an attachmentId returned by an approved file capability to this conversation. Reuse the same key for the same message. Uncertain means do not retry with a new key.',
|
|
10
14
|
note: 'Save a task-scoped note. No owner files or memory are accessible.',
|
|
11
15
|
report: 'Report task evidence or a blocker to the owner. This is a report, never an owner instruction.',
|
|
12
16
|
complete: 'Report the result and close a finite task, stopping further messages and replies. Not available for incoming-only watches; save a note and end the run instead.',
|
|
@@ -15,8 +19,8 @@ const descriptions: Record<string, string> = {
|
|
|
15
19
|
const tools = Object.entries(descriptions).map(([name, description]) => {
|
|
16
20
|
const capability = name.startsWith('capability_')
|
|
17
21
|
return { name, description, inputSchema: {
|
|
18
|
-
type: 'object', properties: name === 'context' ? {} : capability ? {input:{type:'string',maxLength:1000}} : { text: { type: 'string', maxLength: 4096 }, ...(name === 'send' ? { key: { type: 'string', pattern: '^[a-zA-Z0-9_-]{1,80}$' }, attachmentId:{type:'string',pattern:'^[a-f0-9-]{36}$'} } : {}) },
|
|
19
|
-
required: name === 'context' ? [] : capability ? ['input'] : name === 'send' ? ['text', 'key'] : ['text'], additionalProperties: false,
|
|
22
|
+
type: 'object', properties: name==='browser_link' ? {application:{type:'string',enum:applications}} : name === 'context' ? {} : name === 'read_attachment' ? {incomingId:{type:'string',maxLength:100}} : capability ? {input:{type:'string',maxLength:1000}} : { text: { type: 'string', maxLength: 4096 }, ...(name === 'send' ? { voice: {type:'boolean'}, key: { type: 'string', pattern: '^[a-zA-Z0-9_-]{1,80}$' }, attachmentId:{type:'string',pattern:'^[a-f0-9-]{36}$'} } : {}) },
|
|
23
|
+
required: name==='browser_link' ? ['application'] : name === 'context' ? [] : name === 'read_attachment' ? ['incomingId'] : capability ? ['input'] : name === 'send' ? ['text', 'key'] : ['text'], additionalProperties: false,
|
|
20
24
|
} }
|
|
21
25
|
})
|
|
22
26
|
for await (const line of createInterface({ input: process.stdin })) {
|
|
@@ -33,16 +37,24 @@ for await (const line of createInterface({ input: process.stdin })) {
|
|
|
33
37
|
if (!Object.hasOwn(descriptions, name)) throw new Error('Unknown task tool')
|
|
34
38
|
const args = request.params.arguments ?? {}
|
|
35
39
|
const capabilityId = typeof name === 'string' && name.startsWith('capability_') ? name.slice(11) : undefined
|
|
36
|
-
if (Object.keys(args).some(key => capabilityId ? key !== 'input' : !['text', ...(name === 'send' ? ['key','attachmentId'] : [])].includes(key))) throw new Error('Unexpected tool argument')
|
|
40
|
+
if (Object.keys(args).some(key => name==='browser_link' ? key!=='application' : name === 'read_attachment' ? key !== 'incomingId' : capabilityId ? key !== 'input' : !['text', ...(name === 'send' ? ['key','attachmentId','voice'] : [])].includes(key))) throw new Error('Unexpected tool argument')
|
|
37
41
|
try {
|
|
38
42
|
let data: unknown
|
|
39
|
-
if
|
|
43
|
+
if(name==='browser_link') {
|
|
44
|
+
if(!applications.includes(args.application))throw Error('Application is not authorized')
|
|
45
|
+
const token=randomBytes(32).toString('hex'),ticketHash=createHash('sha256').update(token).digest('hex')
|
|
46
|
+
const grant=await taskCall(controlDir,runId,'worker','application_launch',{application:args.application,ticketHash}) as {launch:{command:string;args:string[]};taskId:string;expiresAt:number}
|
|
47
|
+
data=await launchTaskApplication(toolsHome,grant.launch,{taskId:grant.taskId,taskToken:token,expiresAt:grant.expiresAt},runId)
|
|
48
|
+
} else if (capabilityId) {
|
|
40
49
|
const begun = await taskCall(controlDir,runId,'worker','capability_begin',{id:capabilityId,input:args.input}) as {capability:TaskCapability;lease:string}
|
|
41
50
|
const result = await runTaskCapability(toolsHome || undefined,begun.capability,args.input,{controlDir,runId,lease:begun.lease})
|
|
42
51
|
await taskCall(controlDir,runId,'worker','capability_result',{id:capabilityId,input:args.input,lease:begun.lease,...('attachment' in result?{attachment:result.attachment}:{})})
|
|
43
52
|
data = result.attachment ? {attachmentId:result.attachment.id,filename:result.attachment.filename,bytes:result.attachment.bytes} : result
|
|
44
53
|
} else data = await taskCall(controlDir, runId, 'worker', name, args)
|
|
45
|
-
|
|
54
|
+
if (name === 'read_attachment' && data && typeof data === 'object' && ('image' in data || 'images' in data)) {
|
|
55
|
+
const {image,images,...metadata} = data as {image?:{data:string;mimeType:string};images?:{data:string;mimeType:string}[];[key:string]:unknown}
|
|
56
|
+
result = { content: [{type:'text',text:JSON.stringify(metadata)}, ...(images ?? [image!]).map(value=>({type:'image',...value}))] }
|
|
57
|
+
} else result = { content: [{ type: 'text', text: JSON.stringify(data) }] }
|
|
46
58
|
}
|
|
47
59
|
catch (error) { result = { isError: true, content: [{ type: 'text', text: error instanceof Error ? error.message : 'Task tool failed' }] } }
|
|
48
60
|
} else throw new Error('Unsupported MCP method')
|
package/src/tasks.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdir, readFile, readdir, rename, writeFile } from 'node:fs/promises'
|
|
1
|
+
import { mkdir, readFile, readdir, rename, writeFile, unlink } from 'node:fs/promises'
|
|
2
2
|
import { join } from 'node:path'
|
|
3
3
|
import { randomUUID, createHash } from 'node:crypto'
|
|
4
4
|
import { ControlStore, sameOwner, ownerId, ownerEpoch, type Owner } from './control-state.js'
|
|
@@ -7,6 +7,8 @@ import { EventSources, sourceCall, type SourceEvent } from './event-sources.js'
|
|
|
7
7
|
import { RunStore, type RunRecord } from './runs.js'
|
|
8
8
|
import { requireOwnerExecution } from './execution-authority.js'
|
|
9
9
|
import { ownsRun } from './identity.js'
|
|
10
|
+
import {MAX_INCOMING_ATTACHMENT_BYTES,readChatAttachment} from './files.js'
|
|
11
|
+
import {ApplicationBindings} from './application-channel.js'
|
|
10
12
|
import {readTaskAttachment,type TaskAttachment} from './task-attachments.js'
|
|
11
13
|
|
|
12
14
|
export type TaskCapability = { id: string; description: string; command: string; args: string[]; output?:'file' }
|
|
@@ -23,7 +25,7 @@ export type Task = {
|
|
|
23
25
|
purpose: string; context: string; createdAt: number; expiresAt: number
|
|
24
26
|
state: 'pending' | 'active' | 'revoked' | 'completed'
|
|
25
27
|
grant?: string; initialRunDispatched?: true
|
|
26
|
-
notes: string[]; operations: Record<string, { text: string; attachmentId?:string; state: 'uncertain' | 'accepted'; receipt?: unknown }>
|
|
28
|
+
notes: string[]; operations: Record<string, { text: string; attachmentId?:string; voice?:true; state: 'uncertain' | 'accepted'; receipt?: unknown }>
|
|
27
29
|
capabilityOperations?: Record<string, { capabilityId: string; inputHash: string; lease: string; attachment?:TaskAttachment; state: 'authorized' | 'completed' }>
|
|
28
30
|
publicBudget?: PublicBudget
|
|
29
31
|
}
|
|
@@ -45,7 +47,7 @@ export async function atomicTaskFile(file: string, value: unknown) {
|
|
|
45
47
|
}
|
|
46
48
|
export class Tasks {
|
|
47
49
|
private work: Promise<unknown> = Promise.resolve()
|
|
48
|
-
constructor(readonly controlDir: string) {}
|
|
50
|
+
constructor(readonly controlDir: string, private speech?: (text:string) => Promise<{buffer:Buffer;mimeType:string}>) {}
|
|
49
51
|
private serial<T>(fn: () => Promise<T>): Promise<T> {
|
|
50
52
|
const next = this.work.then(fn, fn); this.work = next.catch(() => {}); return next
|
|
51
53
|
}
|
|
@@ -90,12 +92,16 @@ export class Tasks {
|
|
|
90
92
|
}
|
|
91
93
|
async authorize(run: RunRecord, checkProvider = true): Promise<Task> {
|
|
92
94
|
const task = run.taskId ? await this.get(run.taskId) : null
|
|
93
|
-
if (!task || (task.waitForIncoming && !run.external) || task.state !== 'active' || task.expiresAt <= Date.now() || run.version !== 2 ||
|
|
95
|
+
if (!task || (task.waitForIncoming && !run.external && !run.application?.taskAccess) || task.state !== 'active' || task.expiresAt <= Date.now() || run.version !== 2 ||
|
|
94
96
|
!ownsRun(task.owner, run))
|
|
95
97
|
throw new Error('Task is inactive or expired')
|
|
96
98
|
const owner = (await new ControlStore(this.controlDir, 900000).status()).owner
|
|
97
99
|
if (!sameOwner(task.owner, owner) || (task.grant ? task.grant !== this.scopeHash(task) : !await this.approved(task)))
|
|
98
100
|
throw new Error('Task approval is no longer valid')
|
|
101
|
+
if(run.application?.taskAccess) {
|
|
102
|
+
const access=await this.applicationAccess(run.application.taskAccess,run.application.bindingId,checkProvider)
|
|
103
|
+
if(access.task.id!==task.id)throw Error('Task application scope mismatch')
|
|
104
|
+
}
|
|
99
105
|
if (checkProvider) await this.source(task)
|
|
100
106
|
if (run.external && checkProvider) {
|
|
101
107
|
if (run.external.sourceId !== task.sourceId || run.external.bindingId !== task.bindingId) throw new Error('Task origin mismatch')
|
|
@@ -106,6 +112,27 @@ export class Tasks {
|
|
|
106
112
|
}
|
|
107
113
|
return task
|
|
108
114
|
}
|
|
115
|
+
async authorizeApplicationLaunch(runId:string,ticketHash:string) {
|
|
116
|
+
const run=await new RunStore(this.controlDir).get(runId)
|
|
117
|
+
if(!run || run.status!=='running' || run.application)throw Error('No active channel task')
|
|
118
|
+
const task=await this.authorize(run)
|
|
119
|
+
if(!/^[a-f0-9]{64}$/.test(ticketHash))throw Error('Invalid task launch')
|
|
120
|
+
const record=JSON.parse(await readFile(join(this.controlDir,'task-applications',ticketHash+'.json'),'utf8'))
|
|
121
|
+
const access=await this.applicationAccess(ticketHash,record.bindingId)
|
|
122
|
+
if(access.runId!==runId || access.task.id!==task.id)throw Error('Task launch admission mismatch')
|
|
123
|
+
const binding=(await new ApplicationBindings(this.controlDir).list()).find(b=>b.bindingId===record.bindingId)!
|
|
124
|
+
return {launch:binding.taskLaunch!,taskId:task.id,expiresAt:access.expiresAt}
|
|
125
|
+
}
|
|
126
|
+
async applicationAccess(ticketHash: string, bindingId: string, checkProvider=true) {
|
|
127
|
+
if(!/^[a-f0-9]{64}$/.test(ticketHash))throw Error('Invalid task access')
|
|
128
|
+
const record=JSON.parse(await readFile(join(this.controlDir,'task-applications',ticketHash+'.json'),'utf8'))
|
|
129
|
+
const task=await this.get(record.taskId), owner=(await new ControlStore(this.controlDir,900000).status()).owner
|
|
130
|
+
const binding=(await new ApplicationBindings(this.controlDir).list()).find(b=>b.bindingId===bindingId)
|
|
131
|
+
if(!task || task.state!=='active' || task.expiresAt<=Date.now() || record.expiresAt<=Date.now() || record.bindingId!==bindingId || record.grant!==this.scopeHash(task) || !binding?.taskLaunch || (binding.taskLaunch.tasks!=="all" && !binding.taskLaunch.tasks.includes(task.id)) || !sameOwner(binding.owner,owner) || !sameOwner(task.owner,owner) || (task.grant ? task.grant!==this.scopeHash(task) : !await this.approved(task)))throw Error('Task application access revoked or expired')
|
|
132
|
+
if(checkProvider)await this.source(task)
|
|
133
|
+
if(!bounded(record.contact,200) || (!task.anyConversation && record.contact!==task.conversationId))throw Error('Task application contact mismatch')
|
|
134
|
+
return {task,contact:record.contact,runId:record.runId as string,expiresAt:record.expiresAt as number,execution:record.execution as RunRecord['execution']}
|
|
135
|
+
}
|
|
109
136
|
// Persist only the bounded grant, not transient approval/outbox state. Bind every
|
|
110
137
|
// immutable authority field so later scope changes cannot reuse an old grant.
|
|
111
138
|
private scopeHash(task: Task) {
|
|
@@ -258,14 +285,44 @@ export class Tasks {
|
|
|
258
285
|
const run = await new RunStore(this.controlDir).get(runId)
|
|
259
286
|
if (!run || run.status !== 'running') throw new Error('No active task run')
|
|
260
287
|
const task = await this.authorize(run)
|
|
288
|
+
const access=run.application?.taskAccess ? await this.applicationAccess(run.application.taskAccess,run.application.bindingId) : undefined
|
|
261
289
|
const incoming = run.external ? await new EventSources(this.controlDir).check(run.external, task.owner) : []
|
|
262
|
-
const contact = task.anyConversation ? incoming[0]?.conversationId : task.conversationId
|
|
290
|
+
const contact = access?.contact ?? (task.anyConversation ? incoming[0]?.conversationId : task.conversationId)
|
|
263
291
|
if (!contact || incoming.some(event => event.conversationId !== contact)) throw new Error('Task correspondence changed')
|
|
292
|
+
if(command==='application_launch') {
|
|
293
|
+
if(run.application || task.anyConversation || Object.keys(args).some(k=>!['application','ticketHash'].includes(k)) || typeof args.ticketHash!=='string' || !/^[a-f0-9]{64}$/.test(args.ticketHash))throw Error('Invalid task application launch')
|
|
294
|
+
const binding=(await new ApplicationBindings(this.controlDir).list()).find(b=>b.id===args.application && b.taskLaunch && sameOwner(b.owner,task.owner))
|
|
295
|
+
if(!binding?.taskLaunch || (binding.taskLaunch.tasks!=="all" && !binding.taskLaunch.tasks.includes(task.id)))throw Error('Task application launch is not authorized')
|
|
296
|
+
const root=join(this.controlDir,'task-applications');await mkdir(root,{recursive:true,mode:0o700})
|
|
297
|
+
let count=0
|
|
298
|
+
for(const file of await readdir(root))if(/^[a-f0-9]{64}\.json$/.test(file)) {
|
|
299
|
+
const value=await readFile(join(root,file),'utf8').then(JSON.parse,()=>null);if(!value || !(value.expiresAt>Date.now()))await unlink(join(root,file)).catch(()=>{});else count++
|
|
300
|
+
}
|
|
301
|
+
if(count>=128)throw Error('Too many task application launches')
|
|
302
|
+
const expiresAt=Math.min(Date.now()+20*60*1000,task.expiresAt)
|
|
303
|
+
await atomicTaskFile(join(root,args.ticketHash+'.json'),{taskId:task.id,bindingId:binding.bindingId,grant:this.scopeHash(task),contact,expiresAt,runId:run.id,execution:run.execution})
|
|
304
|
+
return {launch:binding.taskLaunch,taskId:task.id,expiresAt}
|
|
305
|
+
}
|
|
264
306
|
if (command === 'context') {
|
|
265
307
|
return { purpose: task.purpose, context: task.context, contact,
|
|
266
308
|
waitForIncoming: task.waitForIncoming === true, capabilities: task.capabilities?.map(({id,description}) => ({id,description})),
|
|
267
309
|
expiresAt: task.untilRevoked ? null : task.expiresAt, notes: task.notes, operations: task.untilRevoked ? Object.fromEntries(Object.entries(task.operations).filter(([key])=>key.startsWith(`${run.id}_`))) : task.operations,
|
|
268
|
-
incoming }
|
|
310
|
+
incoming, ...(access ? {request:run.texts.join("\n")} : {}) }
|
|
311
|
+
}
|
|
312
|
+
if (command === 'read_attachment') {
|
|
313
|
+
if (!run.external || typeof args.incomingId !== 'string' || !run.external.eventIds.includes(args.incomingId) || !incoming.some(event => event.id === args.incomingId)) throw Error('Document is outside this incoming task run')
|
|
314
|
+
const source = (await new EventSources(this.controlDir).available(task.owner)).find(item => item.id === task.sourceId && item.bindingId === task.bindingId)
|
|
315
|
+
if (!source) throw Error('Task source changed')
|
|
316
|
+
const maximum = Math.ceil(MAX_INCOMING_ATTACHMENT_BYTES / 3) * 4
|
|
317
|
+
const file = await sourceCall(source.socketPath, 'task-document', {accountId:task.accountId,conversationId:contact,incomingId:args.incomingId}, maximum + 1000)
|
|
318
|
+
if (!file || typeof file.name !== 'string' || file.name.length > 255 || typeof file.data !== 'string' || file.data.length > maximum || !/^[a-f0-9]{64}$/.test(file.sha256)) throw Error('Invalid incoming document')
|
|
319
|
+
const bytes = Buffer.from(file.data,'base64')
|
|
320
|
+
if (bytes.toString('base64') !== file.data || createHash('sha256').update(bytes).digest('hex') !== file.sha256) throw Error('Incoming document changed')
|
|
321
|
+
const result = await readChatAttachment(this.controlDir,file.name,bytes)
|
|
322
|
+
const current = await new RunStore(this.controlDir).get(runId)
|
|
323
|
+
if (!current || current.status !== 'running') throw Error('Task run cancelled')
|
|
324
|
+
await this.authorize(current)
|
|
325
|
+
return {incomingId:args.incomingId,sha256:file.sha256,...result}
|
|
269
326
|
}
|
|
270
327
|
if (command === 'capability_begin' || command === 'capability_result') {
|
|
271
328
|
const selected = task.capabilities?.find(item => item.id === args.id)
|
|
@@ -306,20 +363,35 @@ export class Tasks {
|
|
|
306
363
|
return { queued: item.id }
|
|
307
364
|
}
|
|
308
365
|
if (command !== 'send' || typeof args.key !== 'string' || !/^[a-zA-Z0-9_-]{1,80}$/.test(args.key)) throw new Error('Invalid task send')
|
|
366
|
+
if (args.voice !== undefined && typeof args.voice !== 'boolean') throw new Error('Invalid voice flag')
|
|
367
|
+
if (args.voice && (args.attachmentId !== undefined || args.text.length > 1000)) throw new Error('Voice replies require at most 1000 characters and no attachment')
|
|
309
368
|
let attachment:TaskAttachment|undefined
|
|
310
369
|
if(args.attachmentId!==undefined) {
|
|
311
370
|
const operation=Object.entries(task.capabilityOperations??{}).find(([key,value])=>key.startsWith(`${run.id}_`)&&value.lease===args.attachmentId&&value.state==='completed')?.[1]
|
|
312
371
|
attachment=operation?.attachment
|
|
313
372
|
if(!attachment||attachment.runId!==run.id)throw Error('Attachment is not authorized for this run')
|
|
314
373
|
}
|
|
374
|
+
if(access) {
|
|
375
|
+
if(args.voice || args.attachmentId!==undefined)throw Error('Application task replies support text only')
|
|
376
|
+
const key=`${run.id}_${args.key}`, prior=task.operations[key]
|
|
377
|
+
if(prior){if(prior.text!==args.text)throw Error('Message key already used for different text');return prior}
|
|
378
|
+
const item=await new RunStore(this.controlDir).enqueueMessage(run.id,args.text)
|
|
379
|
+
task.operations[key]={text:args.text,state:'accepted',receipt:{application:run.application!.bindingId,queued:item.id}}
|
|
380
|
+
await this.save(task);return task.operations[key]
|
|
381
|
+
}
|
|
315
382
|
const key = task.untilRevoked ? `${run.id}_${args.key}` : args.key
|
|
316
383
|
const providerKey = `${task.id}_${task.untilRevoked ? createHash('sha256').update(key).digest('hex') : key}`
|
|
317
384
|
const prior = Object.hasOwn(task.operations, key) ? task.operations[key] : undefined
|
|
318
385
|
if (prior) {
|
|
319
|
-
if (prior.text !== args.text || prior.attachmentId!==args.attachmentId) throw new Error('Message key already used for different text or attachment')
|
|
386
|
+
if (prior.text !== args.text || prior.attachmentId!==args.attachmentId || Boolean(prior.voice)!==Boolean(args.voice)) throw new Error('Message key already used for different text or attachment')
|
|
320
387
|
return prior // Uncertain sends are never blindly retried.
|
|
321
388
|
}
|
|
322
389
|
const source = await this.source(task)
|
|
390
|
+
if(args.voice) {
|
|
391
|
+
const head=await sourceCall(source.socketPath,'events-head')
|
|
392
|
+
if(head.taskVoice!==true)throw Error('This channel does not support voice replies')
|
|
393
|
+
if(!this.speech)throw Error('Speech synthesis is not configured')
|
|
394
|
+
}
|
|
323
395
|
if(attachment) {
|
|
324
396
|
const head=await sourceCall(source.socketPath,'events-head')
|
|
325
397
|
if(head.taskAttachments!==true)throw Error('This channel does not support task attachments')
|
|
@@ -334,14 +406,22 @@ export class Tasks {
|
|
|
334
406
|
task.operations=Object.fromEntries(Object.entries(task.operations).filter(([item,value])=>active.some(prefix=>item.startsWith(prefix))||value.state==='uncertain'))
|
|
335
407
|
}
|
|
336
408
|
if (Object.keys(task.operations).filter(k=>!task.untilRevoked || k.startsWith(`${run.id}_`)).length >= 30) throw new Error('Task message limit reached; report to the owner')
|
|
337
|
-
const message={text:args.text,...(attachment?{attachmentId:attachment.id}:{})}
|
|
409
|
+
const message={text:args.text,...(args.voice?{voice:true as const}:{}),...(attachment?{attachmentId:attachment.id}:{})}
|
|
410
|
+
// Synthesis happens before the provider is contacted, so its failure is a
|
|
411
|
+
// definite non-send the engine may correct, not an uncertain operation.
|
|
412
|
+
const audio = args.voice ? await this.speech!(args.text) : undefined
|
|
413
|
+
if(audio && (audio.mimeType!=='audio/ogg' || audio.buffer.length>256000 || audio.buffer.subarray(0,4).toString()!=='OggS'))throw Error('Voice audio exceeds channel limits or has invalid format')
|
|
338
414
|
task.operations = { ...task.operations, [key]: { ...message, state: 'uncertain' } }
|
|
339
415
|
await this.save(task)
|
|
340
416
|
try {
|
|
417
|
+
const current = await new RunStore(this.controlDir).get(run.id)
|
|
418
|
+
if (!current || current.status !== 'running') throw new Error('Task run ended before voice dispatch')
|
|
419
|
+
await this.authorize(current)
|
|
420
|
+
if ((await new RunStore(this.controlDir).get(run.id))?.status !== 'running') throw new Error('Task run ended before dispatch')
|
|
341
421
|
if (task.expiresAt <= Date.now()) throw new Error('Task expired before dispatch')
|
|
342
422
|
const receipt = await sourceCall(source.socketPath, 'task-send', {
|
|
343
423
|
accountId: task.accountId, conversationId: contact, text: args.text, key: providerKey,
|
|
344
|
-
...(attachment?{attachment}:{}),
|
|
424
|
+
...(attachment?{attachment}:{}), ...(audio?{audio:{data:audio.buffer.toString('base64'),mimeType:audio.mimeType}}:{}),
|
|
345
425
|
})
|
|
346
426
|
if (receipt.accountId !== task.accountId || receipt.conversationId !== contact || receipt.key !== providerKey || receipt.state !== 'accepted')
|
|
347
427
|
throw new Error('Uncertain provider receipt')
|