@pikku/core 0.12.94 → 0.12.95
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/CHANGELOG.md +136 -0
- package/dist/services/email-template.d.ts +43 -0
- package/dist/services/email-template.js +139 -0
- package/dist/services/http-personas.d.ts +6 -1
- package/dist/services/http-personas.js +4 -1
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/wirings/agent/agent-prepare.d.ts +14 -0
- package/dist/wirings/agent/agent-prepare.js +24 -0
- package/dist/wirings/agent/index.d.ts +1 -1
- package/dist/wirings/agent/index.js +1 -1
- package/dist/wirings/scheduler/scheduler-runner.js +0 -1
- package/dist/wirings/virtual-user/index.d.ts +1 -0
- package/dist/wirings/virtual-user/index.js +1 -0
- package/dist/wirings/virtual-user/virtual-user-derive.js +9 -0
- package/dist/wirings/virtual-user/virtual-user-scaffold.d.ts +267 -0
- package/dist/wirings/virtual-user/virtual-user-scaffold.js +400 -0
- package/dist/wirings/workflow/index.d.ts +1 -0
- package/dist/wirings/workflow/index.js +1 -0
- package/dist/wirings/workflow/pikku-workflow-service.js +3 -9
- package/dist/wirings/workflow/workflow-queue-routing.d.ts +18 -0
- package/dist/wirings/workflow/workflow-queue-routing.js +35 -0
- package/dist/wirings/workflow/workflow-status-stream.d.ts +28 -0
- package/dist/wirings/workflow/workflow-status-stream.js +105 -0
- package/package.json +1 -1
- package/src/public-surface.json +17 -1
- package/src/services/email-template.test.ts +311 -0
- package/src/services/email-template.ts +254 -0
- package/src/services/http-personas.ts +10 -2
- package/src/services/index.ts +8 -0
- package/src/services/persona-sign-in.test.ts +22 -0
- package/src/wirings/agent/agent-helpers.test.ts +63 -0
- package/src/wirings/agent/agent-prepare.ts +25 -0
- package/src/wirings/agent/index.ts +1 -0
- package/src/wirings/scheduler/scheduler-runner.test.ts +178 -0
- package/src/wirings/scheduler/scheduler-runner.ts +0 -1
- package/src/wirings/virtual-user/index.ts +20 -0
- package/src/wirings/virtual-user/virtual-user-derive.test.ts +28 -0
- package/src/wirings/virtual-user/virtual-user-derive.ts +9 -0
- package/src/wirings/virtual-user/virtual-user-scaffold.test.ts +795 -0
- package/src/wirings/virtual-user/virtual-user-scaffold.ts +634 -0
- package/src/wirings/workflow/index.ts +4 -0
- package/src/wirings/workflow/pikku-workflow-service.test.ts +71 -2
- package/src/wirings/workflow/pikku-workflow-service.ts +5 -11
- package/src/wirings/workflow/workflow-child-run-session.test.ts +79 -0
- package/src/wirings/workflow/workflow-queue-routing.ts +44 -0
- package/src/wirings/workflow/workflow-status-stream.test.ts +354 -0
- package/src/wirings/workflow/workflow-status-stream.ts +144 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The renderer behind a generated `pikku-emails.gen.ts`.
|
|
3
|
+
*
|
|
4
|
+
* The generated module supplies the assets — theme, locale strings, partials and
|
|
5
|
+
* the templates themselves — and a typed wrapper over `renderEmail`. Everything
|
|
6
|
+
* here is the same for every application, which is why it lives in core rather
|
|
7
|
+
* than in the string the CLI writes: this is HTML escaping, and code inside a
|
|
8
|
+
* template literal is never compiled, never linted, and testable only by
|
|
9
|
+
* matching the text it emits.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface EmailTemplateHashes {
|
|
13
|
+
contentHash: string
|
|
14
|
+
htmlHash: string
|
|
15
|
+
subjectHash: string
|
|
16
|
+
textHash: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface EmailTemplateAssets {
|
|
20
|
+
html: string
|
|
21
|
+
subject: string
|
|
22
|
+
text: string
|
|
23
|
+
variables: ReadonlyArray<string>
|
|
24
|
+
hashes: Record<string, EmailTemplateHashes>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface EmailAssets {
|
|
28
|
+
theme: Record<string, unknown>
|
|
29
|
+
locales: Record<string, Record<string, unknown>>
|
|
30
|
+
partials: Record<string, string>
|
|
31
|
+
templates: Record<string, EmailTemplateAssets>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface RenderEmailRequest {
|
|
35
|
+
name: string
|
|
36
|
+
locale?: string
|
|
37
|
+
data?: Record<string, unknown>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface RenderedEmailResult {
|
|
41
|
+
locale: string
|
|
42
|
+
subject: string
|
|
43
|
+
html: string
|
|
44
|
+
text?: string
|
|
45
|
+
variables: ReadonlyArray<string>
|
|
46
|
+
hash: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const HTML_ESCAPES: Record<string, string> = {
|
|
50
|
+
'&': '&',
|
|
51
|
+
'<': '<',
|
|
52
|
+
'>': '>',
|
|
53
|
+
'"': '"',
|
|
54
|
+
"'": ''',
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const escapeHtml = (value: string): string =>
|
|
58
|
+
value.replace(/[&<>"']/g, (char) => HTML_ESCAPES[char] ?? char)
|
|
59
|
+
|
|
60
|
+
// Matches the raw {{{ value }}} form before the escaped {{ value }} form, so the
|
|
61
|
+
// opt-in escape hatch is never mistaken for a normal substitution.
|
|
62
|
+
const TEMPLATE_TOKEN = /\{\{\{\s*([^{}]+?)\s*\}\}\}|\{\{\s*([^}]+?)\s*\}\}/g
|
|
63
|
+
|
|
64
|
+
const PARTIAL_TOKEN = /\{\{\s*>\s*([a-zA-Z0-9-_/.]+)\s*\}\}/g
|
|
65
|
+
|
|
66
|
+
const MAX_TEMPLATE_DEPTH = 5
|
|
67
|
+
|
|
68
|
+
// theme.json and the locale files ship with the templates, so they are treated as
|
|
69
|
+
// template-author input: expanded before caller data and allowed to contain their
|
|
70
|
+
// own placeholders. Everything else is caller-supplied.
|
|
71
|
+
const TRUSTED_ROOTS = ['theme', 't']
|
|
72
|
+
|
|
73
|
+
const isTrustedKey = (key: string): boolean =>
|
|
74
|
+
TRUSTED_ROOTS.includes(String(key.split('.')[0]))
|
|
75
|
+
|
|
76
|
+
const getNestedValue = (
|
|
77
|
+
source: Record<string, unknown>,
|
|
78
|
+
path: string
|
|
79
|
+
): string => {
|
|
80
|
+
const segments = path.split('.')
|
|
81
|
+
let current: unknown = source
|
|
82
|
+
for (const segment of segments) {
|
|
83
|
+
// `hasOwn`, not `in`: `in` walks the prototype chain, so a path is answered
|
|
84
|
+
// by what an object inherits rather than only by what it carries. Nothing
|
|
85
|
+
// inherited reaches the output today — every step past a prototype hit lands
|
|
86
|
+
// on a function, which is neither traversed nor written — so this closes the
|
|
87
|
+
// lookup rather than fixing a value that escapes through it.
|
|
88
|
+
if (
|
|
89
|
+
!current ||
|
|
90
|
+
typeof current !== 'object' ||
|
|
91
|
+
!Object.hasOwn(current, segment)
|
|
92
|
+
) {
|
|
93
|
+
return ''
|
|
94
|
+
}
|
|
95
|
+
current = (current as Record<string, unknown>)[segment]
|
|
96
|
+
}
|
|
97
|
+
return typeof current === 'string' || typeof current === 'number'
|
|
98
|
+
? String(current)
|
|
99
|
+
: ''
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const readToken = (rawTriple: unknown, rawDouble: unknown) => {
|
|
103
|
+
const raw = typeof rawTriple === 'string'
|
|
104
|
+
return { raw, key: String(raw ? rawTriple : rawDouble).trim() }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const expandPartials = (
|
|
108
|
+
source: string,
|
|
109
|
+
partials: Record<string, string>,
|
|
110
|
+
depth = 0
|
|
111
|
+
): string => {
|
|
112
|
+
if (depth >= MAX_TEMPLATE_DEPTH) return source
|
|
113
|
+
let found = false
|
|
114
|
+
const expanded = source.replace(PARTIAL_TOKEN, (_match, partialName) => {
|
|
115
|
+
found = true
|
|
116
|
+
const partial = partials[String(partialName).trim()]
|
|
117
|
+
return typeof partial === 'string' ? partial : ''
|
|
118
|
+
})
|
|
119
|
+
return found ? expandPartials(expanded, partials, depth + 1) : expanded
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const expandTrusted = (
|
|
123
|
+
source: string,
|
|
124
|
+
context: Record<string, unknown>,
|
|
125
|
+
escape: boolean
|
|
126
|
+
): string => {
|
|
127
|
+
let rendered = source
|
|
128
|
+
for (let i = 0; i < MAX_TEMPLATE_DEPTH; i += 1) {
|
|
129
|
+
let found = false
|
|
130
|
+
const next = rendered.replace(
|
|
131
|
+
TEMPLATE_TOKEN,
|
|
132
|
+
(match, rawTriple, rawDouble) => {
|
|
133
|
+
const { raw, key } = readToken(rawTriple, rawDouble)
|
|
134
|
+
if (!isTrustedKey(key)) return match
|
|
135
|
+
found = true
|
|
136
|
+
const value = getNestedValue(context, key)
|
|
137
|
+
return raw || !escape ? value : escapeHtml(value)
|
|
138
|
+
}
|
|
139
|
+
)
|
|
140
|
+
if (!found || next === rendered) break
|
|
141
|
+
rendered = next
|
|
142
|
+
}
|
|
143
|
+
return rendered
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// A single substitution pass — the replacement text is never rescanned, so a
|
|
147
|
+
// caller-supplied value can never be reinterpreted as a template.
|
|
148
|
+
const substitute = (
|
|
149
|
+
source: string,
|
|
150
|
+
context: Record<string, unknown>,
|
|
151
|
+
escape: boolean,
|
|
152
|
+
slot?: string
|
|
153
|
+
): string =>
|
|
154
|
+
source.replace(TEMPLATE_TOKEN, (_match, rawTriple, rawDouble) => {
|
|
155
|
+
const { raw, key } = readToken(rawTriple, rawDouble)
|
|
156
|
+
// `content` is the layout's slot for the body that was already rendered and
|
|
157
|
+
// escaped, so it is the one value written in raw. Only the layout gets it:
|
|
158
|
+
// honouring it everywhere would let a caller pass `data.content` into a
|
|
159
|
+
// template that happens to name it and have it emitted unescaped.
|
|
160
|
+
if (slot !== undefined && key === slot) {
|
|
161
|
+
return typeof context[slot] === 'string' ? (context[slot] as string) : ''
|
|
162
|
+
}
|
|
163
|
+
if (key.startsWith('>')) {
|
|
164
|
+
return ''
|
|
165
|
+
}
|
|
166
|
+
const value = getNestedValue(context, key)
|
|
167
|
+
return raw || !escape ? value : escapeHtml(value)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const renderTemplate = (
|
|
171
|
+
source: string,
|
|
172
|
+
partials: Record<string, string>,
|
|
173
|
+
context: Record<string, unknown>,
|
|
174
|
+
escape: boolean,
|
|
175
|
+
slot?: string
|
|
176
|
+
): string => {
|
|
177
|
+
const composed = expandTrusted(
|
|
178
|
+
expandPartials(source, partials),
|
|
179
|
+
context,
|
|
180
|
+
escape
|
|
181
|
+
)
|
|
182
|
+
return substitute(composed, context, escape, slot)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export const renderEmail = (
|
|
186
|
+
{ theme, locales, partials, templates }: EmailAssets,
|
|
187
|
+
{ name, locale: requestedLocale, data }: RenderEmailRequest
|
|
188
|
+
): RenderedEmailResult => {
|
|
189
|
+
const locale = requestedLocale ?? 'en'
|
|
190
|
+
const template = templates[name]
|
|
191
|
+
if (!template) {
|
|
192
|
+
throw new Error(`Unknown email template: ${name}`)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const strings = locales[locale]
|
|
196
|
+
if (!strings) {
|
|
197
|
+
throw new Error(`Unknown email locale: ${locale}`)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const values = data ?? {}
|
|
201
|
+
const appName =
|
|
202
|
+
(typeof values.appName === 'string' && values.appName) ||
|
|
203
|
+
getNestedValue(theme, 'appName')
|
|
204
|
+
|
|
205
|
+
const baseContext = {
|
|
206
|
+
...values,
|
|
207
|
+
locale,
|
|
208
|
+
theme,
|
|
209
|
+
t: strings,
|
|
210
|
+
appName,
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const subject = renderTemplate(
|
|
214
|
+
template.subject,
|
|
215
|
+
partials,
|
|
216
|
+
baseContext,
|
|
217
|
+
false
|
|
218
|
+
).trim()
|
|
219
|
+
|
|
220
|
+
const htmlBody = renderTemplate(
|
|
221
|
+
template.html,
|
|
222
|
+
partials,
|
|
223
|
+
{ ...baseContext, subject },
|
|
224
|
+
true
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
const html = partials.layout
|
|
228
|
+
? renderTemplate(
|
|
229
|
+
partials.layout,
|
|
230
|
+
partials,
|
|
231
|
+
{ ...baseContext, subject, content: htmlBody },
|
|
232
|
+
true,
|
|
233
|
+
'content'
|
|
234
|
+
)
|
|
235
|
+
: htmlBody
|
|
236
|
+
|
|
237
|
+
const text = template.text
|
|
238
|
+
? renderTemplate(
|
|
239
|
+
template.text,
|
|
240
|
+
partials,
|
|
241
|
+
{ ...baseContext, subject },
|
|
242
|
+
false
|
|
243
|
+
).trim()
|
|
244
|
+
: undefined
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
locale,
|
|
248
|
+
subject,
|
|
249
|
+
html,
|
|
250
|
+
...(text ? { text } : {}),
|
|
251
|
+
variables: template.variables,
|
|
252
|
+
hash: template.hashes[locale]?.contentHash ?? '',
|
|
253
|
+
}
|
|
254
|
+
}
|
|
@@ -51,7 +51,12 @@ export interface HttpPersonasConfig {
|
|
|
51
51
|
operator?: OperatorSignInOptions
|
|
52
52
|
/** Persona id → the declaration with its address filled in. */
|
|
53
53
|
personas: Record<string, ResolvedPersona>
|
|
54
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* Sign-in path under apiUrl, for whichever of the two paths is in use — an
|
|
56
|
+
* app that mounts auth under `/api` moves both. Default: the actor plugin's
|
|
57
|
+
* `/auth/sign-in/actor`, or `/auth/sign-in/fabric` for an operator.
|
|
58
|
+
* {@link OperatorSignInOptions.signInPath} overrides it.
|
|
59
|
+
*/
|
|
55
60
|
signInPath?: string
|
|
56
61
|
/** Where the session (and its roles) is read back. Default `/auth/get-session`. */
|
|
57
62
|
sessionPath?: string
|
|
@@ -92,7 +97,10 @@ export class HttpPersona implements ScenarioPersona {
|
|
|
92
97
|
) {
|
|
93
98
|
this.jar = createCookieJar(config.apiUrl)
|
|
94
99
|
if (config.operator) {
|
|
95
|
-
this.signIn = new OperatorSignIn(config.apiUrl,
|
|
100
|
+
this.signIn = new OperatorSignIn(config.apiUrl, {
|
|
101
|
+
...config.operator,
|
|
102
|
+
signInPath: config.operator.signInPath ?? config.signInPath,
|
|
103
|
+
})
|
|
96
104
|
} else if (config.secret) {
|
|
97
105
|
this.signIn = new ActorSignIn(
|
|
98
106
|
config.apiUrl,
|
package/src/services/index.ts
CHANGED
|
@@ -53,6 +53,14 @@ export type {
|
|
|
53
53
|
SendTemplateEmailInput,
|
|
54
54
|
SendTextEmailInput,
|
|
55
55
|
} from './email-service.js'
|
|
56
|
+
export {
|
|
57
|
+
renderEmail,
|
|
58
|
+
type EmailAssets,
|
|
59
|
+
type EmailTemplateAssets,
|
|
60
|
+
type EmailTemplateHashes,
|
|
61
|
+
type RenderEmailRequest,
|
|
62
|
+
type RenderedEmailResult,
|
|
63
|
+
} from './email-template.js'
|
|
56
64
|
export {
|
|
57
65
|
DEFAULT_WEBHOOK_RETRIES,
|
|
58
66
|
PIKKU_OUTGOING_WEBHOOK_QUEUE_NAME,
|
|
@@ -115,6 +115,28 @@ describe('operator persona sign-in', () => {
|
|
|
115
115
|
assert.equal(stage.createdCount, 0)
|
|
116
116
|
})
|
|
117
117
|
|
|
118
|
+
// An app that mounts auth somewhere other than the root moves both sign-in
|
|
119
|
+
// paths together, and `SCENARIO_SIGN_IN_PATH` is the only place it can say so.
|
|
120
|
+
test('honours a moved sign-in path the same way the actor path does', async () => {
|
|
121
|
+
const stage = await startStage([
|
|
122
|
+
{ id: 'user-7', email: 'customer@personas.invalid' },
|
|
123
|
+
])
|
|
124
|
+
servers.push(stage.server)
|
|
125
|
+
|
|
126
|
+
const personas = createHttpPersonas({
|
|
127
|
+
apiUrl: stage.apiUrl.replace(/\/api$/, ''),
|
|
128
|
+
operator: { token: OPERATOR_TOKEN },
|
|
129
|
+
signInPath: '/api/auth/sign-in/fabric',
|
|
130
|
+
rpcPath: '/api/rpc',
|
|
131
|
+
personas: { customer: persona('customer@personas.invalid') },
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const result = (await personas.customer!.invoke('whoami', {})) as {
|
|
135
|
+
actingAs: string | null
|
|
136
|
+
}
|
|
137
|
+
assert.equal(result.actingAs, 'user-7')
|
|
138
|
+
})
|
|
139
|
+
|
|
118
140
|
test('refuses a persona the stage has no account for', async () => {
|
|
119
141
|
const stage = await startStage([])
|
|
120
142
|
servers.push(stage.server)
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
agentResume,
|
|
7
7
|
agentApprove,
|
|
8
8
|
} from './agent-helpers.js'
|
|
9
|
+
import { agentCallOptions } from './agent-prepare.js'
|
|
9
10
|
|
|
10
11
|
describe('agent helpers', () => {
|
|
11
12
|
describe('agent', () => {
|
|
@@ -150,3 +151,65 @@ describe('agent helpers', () => {
|
|
|
150
151
|
})
|
|
151
152
|
})
|
|
152
153
|
})
|
|
154
|
+
|
|
155
|
+
describe('agentCallOptions', () => {
|
|
156
|
+
// An explicit `undefined` overrides the agent's own declared default with
|
|
157
|
+
// nothing, so a request that names no model would silently unset the one the
|
|
158
|
+
// agent declares.
|
|
159
|
+
test('a field nobody supplied is left out rather than sent as undefined', () => {
|
|
160
|
+
const options = agentCallOptions({
|
|
161
|
+
message: 'hello',
|
|
162
|
+
threadId: 't1',
|
|
163
|
+
resourceId: 'r1',
|
|
164
|
+
})
|
|
165
|
+
assert.equal('model' in options, false)
|
|
166
|
+
assert.equal('temperature' in options, false)
|
|
167
|
+
assert.equal('context' in options, false)
|
|
168
|
+
assert.equal('attachments' in options, false)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('what was supplied is carried through unchanged', () => {
|
|
172
|
+
const options = agentCallOptions({
|
|
173
|
+
message: 'hello',
|
|
174
|
+
threadId: 't1',
|
|
175
|
+
resourceId: 'user-1',
|
|
176
|
+
model: 'a-model',
|
|
177
|
+
temperature: 0.2,
|
|
178
|
+
context: 'some context',
|
|
179
|
+
attachments: [{ type: 'image' as const, url: 'a.png' }],
|
|
180
|
+
})
|
|
181
|
+
assert.deepEqual(options, {
|
|
182
|
+
message: 'hello',
|
|
183
|
+
threadId: 't1',
|
|
184
|
+
resourceId: 'user-1',
|
|
185
|
+
attachments: [{ type: 'image' as const, url: 'a.png' }],
|
|
186
|
+
model: 'a-model',
|
|
187
|
+
temperature: 0.2,
|
|
188
|
+
context: 'some context',
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
// Zero is a temperature a caller can mean, and the falsy check every other
|
|
193
|
+
// field uses would drop it.
|
|
194
|
+
test('a temperature of zero survives, unlike an empty string', () => {
|
|
195
|
+
assert.equal(
|
|
196
|
+
agentCallOptions({
|
|
197
|
+
message: 'x',
|
|
198
|
+
threadId: 't',
|
|
199
|
+
resourceId: 'r',
|
|
200
|
+
temperature: 0,
|
|
201
|
+
}).temperature,
|
|
202
|
+
0
|
|
203
|
+
)
|
|
204
|
+
assert.equal(
|
|
205
|
+
'context' in
|
|
206
|
+
agentCallOptions({
|
|
207
|
+
message: 'x',
|
|
208
|
+
threadId: 't',
|
|
209
|
+
resourceId: 'r',
|
|
210
|
+
context: '',
|
|
211
|
+
}),
|
|
212
|
+
false
|
|
213
|
+
)
|
|
214
|
+
})
|
|
215
|
+
})
|
|
@@ -149,6 +149,31 @@ export function canAccessThread(
|
|
|
149
149
|
)
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
/**
|
|
153
|
+
* An agent call with the fields nobody supplied left out.
|
|
154
|
+
*
|
|
155
|
+
* Omitted rather than passed as `undefined`, because an explicit `undefined`
|
|
156
|
+
* overrides the agent's own declared default with nothing — a request that
|
|
157
|
+
* names no model would silently unset the one the agent declares.
|
|
158
|
+
*
|
|
159
|
+
* Shared by the scaffolded `run` and `stream` routes, which receive the same
|
|
160
|
+
* input and differ only in what they do with the reply. `agentName` is not part
|
|
161
|
+
* of it: both callers pass that separately, because `rpc.agent.run` and
|
|
162
|
+
* `rpc.agent.stream` take it as their first argument and type the rest
|
|
163
|
+
* against it.
|
|
164
|
+
*/
|
|
165
|
+
export const agentCallOptions = (input: AgentInput): AgentInput => ({
|
|
166
|
+
message: input.message,
|
|
167
|
+
threadId: input.threadId,
|
|
168
|
+
resourceId: input.resourceId,
|
|
169
|
+
...(input.attachments ? { attachments: input.attachments } : {}),
|
|
170
|
+
...(input.model ? { model: input.model } : {}),
|
|
171
|
+
...(input.temperature !== undefined
|
|
172
|
+
? { temperature: input.temperature }
|
|
173
|
+
: {}),
|
|
174
|
+
...(input.context ? { context: input.context } : {}),
|
|
175
|
+
})
|
|
176
|
+
|
|
152
177
|
export type StreamAgentOptions = {
|
|
153
178
|
requiresToolApproval?: 'all' | 'explicit' | false
|
|
154
179
|
onRunCreated?: (runId: string) => void
|
|
@@ -213,6 +213,97 @@ describe('runScheduledTask', () => {
|
|
|
213
213
|
assert.deepEqual(receivedSession, session)
|
|
214
214
|
})
|
|
215
215
|
|
|
216
|
+
test('a task middleware can set the session the task runs as', async () => {
|
|
217
|
+
let frozenSession: CoreUserSession | undefined
|
|
218
|
+
const machineSession: CoreUserSession = {
|
|
219
|
+
userId: 'cron:machine-session-task',
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const mockTask: CoreScheduledTask = {
|
|
223
|
+
name: 'machine-session-task',
|
|
224
|
+
schedule: '0 0 * * *',
|
|
225
|
+
middleware: [
|
|
226
|
+
async (_services: any, wire: any, next: any) => {
|
|
227
|
+
wire.setSession(machineSession)
|
|
228
|
+
return next()
|
|
229
|
+
},
|
|
230
|
+
] as any,
|
|
231
|
+
func: {
|
|
232
|
+
func: async (_services: any, _data: any, wire: any) => {
|
|
233
|
+
frozenSession = wire.session
|
|
234
|
+
},
|
|
235
|
+
auth: false,
|
|
236
|
+
},
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
pikkuState(null, 'scheduler', 'meta')['machine-session-task'] = {
|
|
240
|
+
pikkuFuncId: 'scheduler_machine-session-task',
|
|
241
|
+
name: 'machine-session-task',
|
|
242
|
+
schedule: '0 0 * * *',
|
|
243
|
+
}
|
|
244
|
+
pikkuState(null, 'function', 'meta')['scheduler_machine-session-task'] = {
|
|
245
|
+
pikkuFuncId: 'scheduler_machine-session-task',
|
|
246
|
+
inputSchemaName: null,
|
|
247
|
+
outputSchemaName: null,
|
|
248
|
+
sessionless: true,
|
|
249
|
+
}
|
|
250
|
+
wireScheduler(mockTask)
|
|
251
|
+
|
|
252
|
+
pikkuState(null, 'package', 'singletonServices', {
|
|
253
|
+
logger: createMockLogger(),
|
|
254
|
+
} as any)
|
|
255
|
+
|
|
256
|
+
await runScheduledTask({ name: 'machine-session-task' })
|
|
257
|
+
|
|
258
|
+
assert.deepEqual(frozenSession, machineSession)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('a session-taking task does not warn that auth was disabled', async () => {
|
|
262
|
+
const mockTask: CoreScheduledTask = {
|
|
263
|
+
name: 'identified-task',
|
|
264
|
+
schedule: '0 0 * * *',
|
|
265
|
+
middleware: [
|
|
266
|
+
async (_services: any, wire: any, next: any) => {
|
|
267
|
+
wire.setSession({ userId: 'cron:identified-task' })
|
|
268
|
+
return next()
|
|
269
|
+
},
|
|
270
|
+
] as any,
|
|
271
|
+
func: {
|
|
272
|
+
func: async () => {},
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
pikkuState(null, 'scheduler', 'meta')['identified-task'] = {
|
|
277
|
+
pikkuFuncId: 'scheduler_identified-task',
|
|
278
|
+
name: 'identified-task',
|
|
279
|
+
schedule: '0 0 * * *',
|
|
280
|
+
}
|
|
281
|
+
pikkuState(null, 'function', 'meta')['scheduler_identified-task'] = {
|
|
282
|
+
pikkuFuncId: 'scheduler_identified-task',
|
|
283
|
+
inputSchemaName: null,
|
|
284
|
+
outputSchemaName: null,
|
|
285
|
+
sessionless: false,
|
|
286
|
+
}
|
|
287
|
+
wireScheduler(mockTask)
|
|
288
|
+
|
|
289
|
+
const mockLogger = createMockLogger()
|
|
290
|
+
pikkuState(null, 'package', 'singletonServices', {
|
|
291
|
+
logger: mockLogger,
|
|
292
|
+
} as any)
|
|
293
|
+
|
|
294
|
+
await runScheduledTask({ name: 'identified-task' })
|
|
295
|
+
|
|
296
|
+
assert.deepEqual(
|
|
297
|
+
mockLogger
|
|
298
|
+
.getLogs()
|
|
299
|
+
.filter(
|
|
300
|
+
(log) =>
|
|
301
|
+
log.level === 'warn' && /auth was explicitly disabled/.test(log.message)
|
|
302
|
+
),
|
|
303
|
+
[]
|
|
304
|
+
)
|
|
305
|
+
})
|
|
306
|
+
|
|
216
307
|
test('should throw ScheduledTaskNotFoundError when task not found', async () => {
|
|
217
308
|
const mockLogger = createMockLogger()
|
|
218
309
|
pikkuState(null, 'package', 'singletonServices', {
|
|
@@ -655,3 +746,90 @@ describe('getScheduledTasks', () => {
|
|
|
655
746
|
assert.equal(tasks.size, 0)
|
|
656
747
|
})
|
|
657
748
|
})
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* A cron has no caller to authenticate, so the only thing that can give it an
|
|
752
|
+
* identity is its own wiring. These cover the mechanism the virtual-user
|
|
753
|
+
* scaffold's `virtualUserPlatformSession` relies on: middleware on the task
|
|
754
|
+
* sets the session, and the scope gate on the function it drives is enforced
|
|
755
|
+
* against exactly that session — a tick with no identity, or one holding the
|
|
756
|
+
* wrong scope, is refused the same way a person would be.
|
|
757
|
+
*/
|
|
758
|
+
describe('a scheduled task authorizes on the session its own middleware sets', () => {
|
|
759
|
+
const wireGatedTask = (
|
|
760
|
+
name: string,
|
|
761
|
+
middleware?: Array<
|
|
762
|
+
(services: any, wire: any, next: any) => Promise<void> | void
|
|
763
|
+
>
|
|
764
|
+
) => {
|
|
765
|
+
let seen: CoreUserSession | undefined
|
|
766
|
+
const task: CoreScheduledTask = {
|
|
767
|
+
name,
|
|
768
|
+
schedule: '0 * * * *',
|
|
769
|
+
func: {
|
|
770
|
+
func: async (_services: any, _data: any, wire: any) => {
|
|
771
|
+
seen = await wire.getSession()
|
|
772
|
+
},
|
|
773
|
+
} as any,
|
|
774
|
+
middleware,
|
|
775
|
+
}
|
|
776
|
+
pikkuState(null, 'scheduler', 'meta')[name] = {
|
|
777
|
+
pikkuFuncId: `scheduler_${name}`,
|
|
778
|
+
name,
|
|
779
|
+
schedule: '0 * * * *',
|
|
780
|
+
}
|
|
781
|
+
pikkuState(null, 'function', 'meta')[`scheduler_${name}`] = {
|
|
782
|
+
pikkuFuncId: `scheduler_${name}`,
|
|
783
|
+
inputSchemaName: null,
|
|
784
|
+
outputSchemaName: null,
|
|
785
|
+
sessionless: false,
|
|
786
|
+
scopes: ['virtualUser:run'],
|
|
787
|
+
}
|
|
788
|
+
wireScheduler(task)
|
|
789
|
+
pikkuState(null, 'package', 'singletonServices', {
|
|
790
|
+
logger: createMockLogger(),
|
|
791
|
+
} as any)
|
|
792
|
+
return () => seen
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const settingSession = (session: CoreUserSession) => [
|
|
796
|
+
async (_services: any, wire: any, next: any) => {
|
|
797
|
+
await wire.setSession(session)
|
|
798
|
+
return next()
|
|
799
|
+
},
|
|
800
|
+
]
|
|
801
|
+
|
|
802
|
+
test('is refused outright when nothing gives the tick an identity', async () => {
|
|
803
|
+
wireGatedTask('unidentified-tick')
|
|
804
|
+
await assert.rejects(
|
|
805
|
+
runScheduledTask({ name: 'unidentified-tick' }),
|
|
806
|
+
/Authentication required/
|
|
807
|
+
)
|
|
808
|
+
})
|
|
809
|
+
|
|
810
|
+
test('is refused when the identity it sets holds the wrong scope', async () => {
|
|
811
|
+
wireGatedTask(
|
|
812
|
+
'misscoped-tick',
|
|
813
|
+
settingSession({
|
|
814
|
+
userId: 'pikku-platform',
|
|
815
|
+
scopes: ['admin'],
|
|
816
|
+
} as CoreUserSession)
|
|
817
|
+
)
|
|
818
|
+
await assert.rejects(
|
|
819
|
+
runScheduledTask({ name: 'misscoped-tick' }),
|
|
820
|
+
/virtualUser:run/
|
|
821
|
+
)
|
|
822
|
+
})
|
|
823
|
+
|
|
824
|
+
test('runs as the platform user its middleware set', async () => {
|
|
825
|
+
const seen = wireGatedTask(
|
|
826
|
+
'platform-tick',
|
|
827
|
+
settingSession({
|
|
828
|
+
userId: 'pikku-platform',
|
|
829
|
+
scopes: ['virtualUser:run'],
|
|
830
|
+
} as CoreUserSession)
|
|
831
|
+
)
|
|
832
|
+
await runScheduledTask({ name: 'platform-tick' })
|
|
833
|
+
assert.equal(seen()?.userId, 'pikku-platform')
|
|
834
|
+
})
|
|
835
|
+
})
|
|
@@ -116,7 +116,6 @@ export async function runScheduledTask({
|
|
|
116
116
|
await runPikkuFunc('scheduler', meta.name, meta.pikkuFuncId, {
|
|
117
117
|
singletonServices,
|
|
118
118
|
createWireServices,
|
|
119
|
-
auth: false,
|
|
120
119
|
data: () => undefined,
|
|
121
120
|
inheritedMiddleware: meta.middleware,
|
|
122
121
|
wireMiddleware: task.middleware,
|
|
@@ -85,3 +85,23 @@ export {
|
|
|
85
85
|
personaVirtualUserTarget,
|
|
86
86
|
type PersonaTargetOptions,
|
|
87
87
|
} from './virtual-user-target.js'
|
|
88
|
+
export {
|
|
89
|
+
executeVirtualUserRun,
|
|
90
|
+
logVirtualUserTick,
|
|
91
|
+
requireVirtualUserRunStore,
|
|
92
|
+
requireVirtualUserScheduleStore,
|
|
93
|
+
runnablePersona,
|
|
94
|
+
serializeVirtualUserRun,
|
|
95
|
+
serializeVirtualUserSchedule,
|
|
96
|
+
serializeVirtualUserSteps,
|
|
97
|
+
signInPathFor,
|
|
98
|
+
startVirtualUserRun,
|
|
99
|
+
VIRTUAL_USER_VARIABLES,
|
|
100
|
+
virtualUserScheduleRunInput,
|
|
101
|
+
writeVirtualUserSchedule,
|
|
102
|
+
type ExecuteVirtualUserRunParams,
|
|
103
|
+
type ScaffoldPersonas,
|
|
104
|
+
type StartedVirtualUserRun,
|
|
105
|
+
type StartVirtualUserRunParams,
|
|
106
|
+
type WriteVirtualUserScheduleParams,
|
|
107
|
+
} from './virtual-user-scaffold.js'
|
|
@@ -396,3 +396,31 @@ describe('driving a virtual user through a signed-in actor', () => {
|
|
|
396
396
|
assert.match(verdict.reasoning, /no assistant called 'concierge'/)
|
|
397
397
|
})
|
|
398
398
|
})
|
|
399
|
+
|
|
400
|
+
// An adversarial run's transcript is working exploits against this same app,
|
|
401
|
+
// and a schedule outlives the run that wrote it. Neither belongs in the hands
|
|
402
|
+
// of the thing being run.
|
|
403
|
+
test('a virtual user is never offered the machinery that runs virtual users', () => {
|
|
404
|
+
const catalogue = deriveCatalogue({
|
|
405
|
+
runVirtualUser: {
|
|
406
|
+
name: 'runVirtualUser',
|
|
407
|
+
expose: true,
|
|
408
|
+
scopes: ['virtualUser:run'],
|
|
409
|
+
},
|
|
410
|
+
getVirtualUserRunSteps: {
|
|
411
|
+
name: 'getVirtualUserRunSteps',
|
|
412
|
+
expose: true,
|
|
413
|
+
scopes: ['virtualUser:read'],
|
|
414
|
+
},
|
|
415
|
+
listBookings: {
|
|
416
|
+
name: 'listBookings',
|
|
417
|
+
expose: true,
|
|
418
|
+
scopes: ['bookings:read'],
|
|
419
|
+
},
|
|
420
|
+
} as any)
|
|
421
|
+
|
|
422
|
+
assert.deepEqual(
|
|
423
|
+
catalogue.map((entry) => entry.name),
|
|
424
|
+
['listBookings']
|
|
425
|
+
)
|
|
426
|
+
})
|