@deepagents/context 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +67 -2
  2. package/dist/browser.js +0 -19
  3. package/dist/browser.js.map +2 -2
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +4544 -3346
  7. package/dist/index.js.map +4 -4
  8. package/dist/lib/advisor.d.ts +26 -0
  9. package/dist/lib/advisor.d.ts.map +1 -0
  10. package/dist/lib/agent.d.ts +16 -2
  11. package/dist/lib/agent.d.ts.map +1 -1
  12. package/dist/lib/chat.d.ts +6 -2
  13. package/dist/lib/chat.d.ts.map +1 -1
  14. package/dist/lib/engine.d.ts +10 -0
  15. package/dist/lib/engine.d.ts.map +1 -1
  16. package/dist/lib/fragments.d.ts +0 -40
  17. package/dist/lib/fragments.d.ts.map +1 -1
  18. package/dist/lib/guardrail.d.ts +1 -0
  19. package/dist/lib/guardrail.d.ts.map +1 -1
  20. package/dist/lib/guardrails/error-recovery.guardrail.d.ts.map +1 -1
  21. package/dist/lib/repair.d.ts +4 -0
  22. package/dist/lib/repair.d.ts.map +1 -0
  23. package/dist/lib/sandbox/ast-utils.d.ts +4 -0
  24. package/dist/lib/sandbox/ast-utils.d.ts.map +1 -0
  25. package/dist/lib/sandbox/bash-exception.d.ts +5 -0
  26. package/dist/lib/sandbox/bash-exception.d.ts.map +1 -0
  27. package/dist/lib/sandbox/bash-meta.d.ts +13 -0
  28. package/dist/lib/sandbox/bash-meta.d.ts.map +1 -0
  29. package/dist/lib/sandbox/bash-tool.d.ts +27 -0
  30. package/dist/lib/sandbox/bash-tool.d.ts.map +1 -0
  31. package/dist/lib/sandbox/container-tool.d.ts +39 -11
  32. package/dist/lib/sandbox/container-tool.d.ts.map +1 -1
  33. package/dist/lib/sandbox/extension.d.ts +39 -0
  34. package/dist/lib/sandbox/extension.d.ts.map +1 -0
  35. package/dist/lib/sandbox/file-events.d.ts +47 -0
  36. package/dist/lib/sandbox/file-events.d.ts.map +1 -0
  37. package/dist/lib/sandbox/index.d.ts +14 -0
  38. package/dist/lib/sandbox/index.d.ts.map +1 -1
  39. package/dist/lib/sandbox/openapi/demo.d.ts +2 -0
  40. package/dist/lib/sandbox/openapi/demo.d.ts.map +1 -0
  41. package/dist/lib/sandbox/openapi/extension.d.ts +19 -0
  42. package/dist/lib/sandbox/openapi/extension.d.ts.map +1 -0
  43. package/dist/lib/sandbox/openapi/format.d.ts +11 -0
  44. package/dist/lib/sandbox/openapi/format.d.ts.map +1 -0
  45. package/dist/lib/sandbox/openapi/schema.d.ts +9 -0
  46. package/dist/lib/sandbox/openapi/schema.d.ts.map +1 -0
  47. package/dist/lib/sandbox/openapi/skill/index.d.ts +6 -0
  48. package/dist/lib/sandbox/openapi/skill/index.d.ts.map +1 -0
  49. package/dist/lib/sandbox/openapi/skill/index.js +14 -0
  50. package/dist/lib/sandbox/openapi/skill/index.js.map +7 -0
  51. package/dist/lib/sandbox/openapi/skill/openapi-cli/SKILL.md +109 -0
  52. package/dist/lib/sandbox/openapi/validate.d.ts +7 -0
  53. package/dist/lib/sandbox/openapi/validate.d.ts.map +1 -0
  54. package/dist/lib/sandbox/routing-sandbox.d.ts +28 -0
  55. package/dist/lib/sandbox/routing-sandbox.d.ts.map +1 -0
  56. package/dist/lib/sandbox/subcommand.d.ts +49 -0
  57. package/dist/lib/sandbox/subcommand.d.ts.map +1 -0
  58. package/dist/lib/sandbox/types.d.ts +46 -0
  59. package/dist/lib/sandbox/types.d.ts.map +1 -0
  60. package/dist/lib/sandbox/upload-skills.d.ts +14 -0
  61. package/dist/lib/sandbox/upload-skills.d.ts.map +1 -0
  62. package/dist/lib/sandbox/walk.d.ts +17 -0
  63. package/dist/lib/sandbox/walk.d.ts.map +1 -0
  64. package/dist/lib/skills/fragments.d.ts +10 -15
  65. package/dist/lib/skills/fragments.d.ts.map +1 -1
  66. package/dist/lib/skills/types.d.ts +2 -27
  67. package/dist/lib/skills/types.d.ts.map +1 -1
  68. package/dist/lib/title.d.ts +1 -1
  69. package/dist/lib/title.d.ts.map +1 -1
  70. package/dist/lib/tracing/exporter.d.ts.map +1 -1
  71. package/dist/lib/tracing/index.js +59 -12
  72. package/dist/lib/tracing/index.js.map +2 -2
  73. package/dist/lib/tracing/openai-traces-integration.d.ts.map +1 -1
  74. package/dist/lib/tracing/serialization.d.ts.map +1 -1
  75. package/dist/lib/tracing/types.d.ts +0 -1
  76. package/dist/lib/tracing/types.d.ts.map +1 -1
  77. package/package.json +22 -10
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension.d.ts","sourceRoot":"","sources":["../../../../src/lib/sandbox/openapi/extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAO,MAAM,aAAa,CAAC;AAC/C,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAI1B,MAAM,cAAc,CAAC;AAEtB,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,iBAAiB,CAAC;AAWzB,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC5C,gBAAgB,CAAC,EAAE,CACjB,KAAK,EAAE,cAAc,EACrB,SAAS,EAAE,oBAAoB,KAC5B,OAAO,CAAC;CACd;AAED,MAAM,WAAW,gBAAiB,SAAQ,gBAAgB;IACxD,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B,gBAAgB,EAAE,YAAY,CAAC;IAC/B,MAAM,EAAE,MAAM,CAAC;CAChB;AA8BD,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,gBAAgB,CAAC,CA8J3B"}
@@ -0,0 +1,11 @@
1
+ import type { CommandResult } from 'bash-tool';
2
+ export declare function formatResponse(value: unknown): CommandResult;
3
+ export interface ErrorDetails {
4
+ group: string;
5
+ operation?: string;
6
+ code: string;
7
+ message: string;
8
+ extra?: Record<string, unknown>;
9
+ }
10
+ export declare function formatError(details: ErrorDetails): CommandResult;
11
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../../src/lib/sandbox/openapi/format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAU5D;AAcD,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,aAAa,CAYhE"}
@@ -0,0 +1,9 @@
1
+ import type { Client } from '@sdk-it/rpc';
2
+ import type { SubcommandDefinition } from '../subcommand.ts';
3
+ export interface SchemaEntry {
4
+ endpoint: string;
5
+ operationId: string;
6
+ summary?: string;
7
+ }
8
+ export declare function buildSchemaSubcommand(group: string, client: Client, entries: SchemaEntry[]): SubcommandDefinition;
9
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../src/lib/sandbox/openapi/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAG7D,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,WAAW,EAAE,GACrB,oBAAoB,CAoCtB"}
@@ -0,0 +1,6 @@
1
+ import type { SkillUploadInput } from '../../types.ts';
2
+ export interface OpenAPISkillOptions {
3
+ sandbox?: string;
4
+ }
5
+ export declare function openapiSkill(options?: OpenAPISkillOptions): SkillUploadInput;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/lib/sandbox/openapi/skill/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAIvD,MAAM,WAAW,mBAAmB;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,YAAY,CAC1B,OAAO,GAAE,mBAAwB,GAChC,gBAAgB,CAKlB"}
@@ -0,0 +1,14 @@
1
+ // packages/context/src/lib/sandbox/openapi/skill/index.ts
2
+ import { dirname } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ var here = dirname(fileURLToPath(import.meta.url));
5
+ function openapiSkill(options = {}) {
6
+ return {
7
+ host: here,
8
+ sandbox: options.sandbox ?? "/skills/openapi"
9
+ };
10
+ }
11
+ export {
12
+ openapiSkill
13
+ };
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/lib/sandbox/openapi/skill/index.ts"],
4
+ "sourcesContent": ["import { dirname } from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\nimport type { SkillUploadInput } from '../../types.ts';\n\nconst here = dirname(fileURLToPath(import.meta.url));\n\nexport interface OpenAPISkillOptions {\n sandbox?: string;\n}\n\nexport function openapiSkill(\n options: OpenAPISkillOptions = {},\n): SkillUploadInput {\n return {\n host: here,\n sandbox: options.sandbox ?? '/skills/openapi',\n };\n}\n"],
5
+ "mappings": ";AAAA,SAAS,eAAe;AACxB,SAAS,qBAAqB;AAI9B,IAAM,OAAO,QAAQ,cAAc,YAAY,GAAG,CAAC;AAM5C,SAAS,aACd,UAA+B,CAAC,GACd;AAClB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS,QAAQ,WAAW;AAAA,EAC9B;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,109 @@
1
+ ---
2
+ name: openapi-cli
3
+ description: Use this skill any time the user asks to list, inspect, call, or use commands/operations of a bash-exposed API extension (e.g. datahub, github, stripe). These extensions are NOT standard CLIs — they do NOT support `--help` or `-h`. Each extension exposes one bash command (the "group") with a `schema` subcommand for discovery and operations invoked as `<group> <operation> '<json>'`. ALWAYS run `<group> schema` as the first action before doing anything else; never run `<group> --help` or `<group> -h` (they will fail).
4
+ ---
5
+
6
+ # Invoking OpenAPI extensions
7
+
8
+ Each `createOpenAPIExtension` registers one bash command (the **group**) under which every OpenAPI operation becomes a **subcommand**. You invoke operations through the bash tool. There is no separate API surface.
9
+
10
+ ## Always discover first
11
+
12
+ Before calling any operation, run the `schema` subcommand once per session to learn what's available:
13
+
14
+ ```
15
+ <group> schema
16
+ ```
17
+
18
+ The output is a single JSON document:
19
+
20
+ ```json
21
+ {
22
+ "group": "<group>",
23
+ "operations": [
24
+ {
25
+ "operationId": "...",
26
+ "method": "GET|POST|PUT|PATCH|DELETE",
27
+ "path": "/example/{id}",
28
+ "summary": "...",
29
+ "input": {
30
+ /* JSON Schema 7 */
31
+ }
32
+ }
33
+ ]
34
+ }
35
+ ```
36
+
37
+ Read `input` to learn the exact field names, types, required keys, and enums for that operation. Do not guess fields.
38
+
39
+ ## Invocation shape
40
+
41
+ ```
42
+ <group> <operationId> '<json>'
43
+ ```
44
+
45
+ - **One positional JSON argument.** Wrap it in single quotes so the shell parser treats it as one literal token.
46
+ - **Flat object.** Pass every field at the top level — path params, query params, body fields, headers all live as flat keys in one object. The extension partitions them automatically based on the spec.
47
+ - **No `--flags`.** There are no per-field flags. The whole payload is the JSON.
48
+
49
+ Example:
50
+
51
+ ```
52
+ github listIssues '{"owner":"acme","repo":"app","state":"open","per_page":10}'
53
+ ```
54
+
55
+ `owner` and `repo` are path params; `state` and `per_page` are query params. You don't manage that split — the extension does.
56
+
57
+ ## Output format
58
+
59
+ - **Object response** → one line of JSON on stdout.
60
+ - **Array response** → newline-delimited JSON (NDJSON), one item per line. Pipe to `head -n N` to cap before reading the whole list.
61
+ - **Errors** → JSON on stderr, exit code 1.
62
+
63
+ ```bash
64
+ <group> <operation> '<json>' | head -n 5 # cap arrays
65
+ ```
66
+
67
+ ## Reading errors
68
+
69
+ Stderr on failure is always JSON with this shape:
70
+
71
+ ```json
72
+ {
73
+ "ok": false,
74
+ "group": "...",
75
+ "operation": "...",
76
+ "code": "...",
77
+ "message": "..."
78
+ }
79
+ ```
80
+
81
+ Branch on `code`:
82
+
83
+ | Code | What it means | What to do |
84
+ | ------------------- | ------------------------------------------------- | --------------------------------------------------------------------------- |
85
+ | `missing_input` | No JSON arg passed | Add `'<json>'` |
86
+ | `invalid_json` | JSON didn't parse | Fix the JSON literal |
87
+ | `schema_validation` | Fields don't match the schema | Re-read `<group> schema` and fix shape; `issues` array lists each violation |
88
+ | `path_param_unsafe` | A path-param value contains a forbidden character | See "Path-param rules" below |
89
+ | `request_failed` | The upstream API errored or network failed | Read `message`; surface to the user |
90
+
91
+ ## Path-param rules
92
+
93
+ Path-param values must be **plain segment identifiers**. The extension rejects, _before_ sending the request:
94
+
95
+ - `/`, `\`, `?`, `#`, `&` (would change the route or query)
96
+ - `.` and `..` as full values (would normalize away)
97
+ - Control characters (`\x00`–`\x1f`, `\x7f`)
98
+ - Pre-encoded sequences (`%2e`, `%2f`, etc. — they get re-decoded)
99
+
100
+ If the user gives you something like `abc/def` for a single id, that's two segments — clarify with the user instead of forcing it through.
101
+
102
+ ## Workflow
103
+
104
+ 1. Run `<group> schema` once.
105
+ 2. Find the operation that matches the user's intent (`summary`, `path`, `method`).
106
+ 3. Build the JSON payload from the `input` schema.
107
+ 4. Invoke `<group> <operationId> '<json>'`.
108
+ 5. If `code: "schema_validation"`, read `issues[]` and fix exactly what it says.
109
+ 6. For list endpoints, prefer `| head -n N` over reading all rows.
@@ -0,0 +1,7 @@
1
+ export declare function extractPathParams(path: string): string[];
2
+ export interface UnsafePathParam {
3
+ field: string;
4
+ reason: string;
5
+ }
6
+ export declare function findUnsafePathParam(input: unknown, names: string[]): UnsafePathParam | null;
7
+ //# sourceMappingURL=validate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../../src/lib/sandbox/openapi/validate.ts"],"names":[],"mappings":"AAAA,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAQxD;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAMD,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,MAAM,EAAE,GACd,eAAe,GAAG,IAAI,CAsCxB"}
@@ -0,0 +1,28 @@
1
+ import type { Sandbox } from 'bash-tool';
2
+ import { type IFileSystem } from 'just-bash';
3
+ import { type MergedSandboxExtension, type SandboxExtension } from './extension.ts';
4
+ export interface InstallableSandbox extends Sandbox {
5
+ install(ext: MergedSandboxExtension): void | Promise<void>;
6
+ }
7
+ export declare function isInstallable(s: Sandbox): s is InstallableSandbox;
8
+ export interface CreateVirtualSandboxOptions {
9
+ fs: IFileSystem;
10
+ cwd?: string;
11
+ env?: Record<string, string>;
12
+ }
13
+ export declare function createVirtualSandbox(options: CreateVirtualSandboxOptions): Promise<InstallableSandbox>;
14
+ export interface CreateRoutingSandboxOptions {
15
+ /**
16
+ * Backend sandbox to wrap. Single-use — the same `InstallableSandbox`
17
+ * cannot be passed to two routing sandboxes (install() is install-once).
18
+ */
19
+ backend: Sandbox;
20
+ hostExtensions: SandboxExtension[];
21
+ /**
22
+ * Working directory surfaced to host handlers via `ctx.cwd` on shallow
23
+ * dispatch. Defaults to `/`. Deep dispatch uses the backend's own cwd.
24
+ */
25
+ cwd?: string;
26
+ }
27
+ export declare function createRoutingSandbox(opts: CreateRoutingSandboxOptions): Promise<Sandbox>;
28
+ //# sourceMappingURL=routing-sandbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing-sandbox.d.ts","sourceRoot":"","sources":["../../../src/lib/sandbox/routing-sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAIL,KAAK,WAAW,EAGjB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,gBAAgB,EAEtB,MAAM,gBAAgB,CAAC;AAExB,MAAM,WAAW,kBAAmB,SAAQ,OAAO;IACjD,OAAO,CAAC,GAAG,EAAE,sBAAsB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,kBAAkB,CAEjE;AAED,MAAM,WAAW,2BAA2B;IAC1C,EAAE,EAAE,WAAW,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9B;AAED,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,2BAA2B,GACnC,OAAO,CAAC,kBAAkB,CAAC,CA6D7B;AAED,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,EAAE,gBAAgB,EAAE,CAAC;IACnC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,2BAA2B,GAChC,OAAO,CAAC,OAAO,CAAC,CASlB"}
@@ -0,0 +1,49 @@
1
+ import type { CommandResult } from 'bash-tool';
2
+ import type { ExtensionCommand, ExtensionCommandContext } from './extension.ts';
3
+ /**
4
+ * Describes one subcommand inside a subcommand group (e.g. `sql run`,
5
+ * `sql validate`). Each subcommand has usage/description strings used to
6
+ * generate the group's usage message, an optional arg-repair function that
7
+ * fixes common LLM over-escaping, and the handler that implements it.
8
+ */
9
+ export interface SubcommandDefinition {
10
+ usage: string;
11
+ description: string;
12
+ /**
13
+ * Optional repair pass applied to the subcommand's raw arg string before
14
+ * the shell parser sees it. Return `null` to leave the raw args unchanged.
15
+ */
16
+ repair?: (rawArgs: string) => string | null;
17
+ handler: (args: string[], ctx: ExtensionCommandContext) => CommandResult | Promise<CommandResult>;
18
+ }
19
+ /**
20
+ * Creates an {@link ExtensionCommand} that dispatches to one of several
21
+ * subcommand handlers based on the first positional arg. Unknown or missing
22
+ * subcommands print an auto-generated usage message to stderr with exit
23
+ * code 1.
24
+ *
25
+ * @example
26
+ * const sqlCommand = defineSubcommandGroup('sql', {
27
+ * run: { usage: 'run "SELECT ..."', description: 'Execute query', handler: ... },
28
+ * validate: { usage: 'validate "SELECT ..."', description: 'Validate only', handler: ... },
29
+ * });
30
+ */
31
+ export declare function defineSubcommandGroup(name: string, subcommands: Record<string, SubcommandDefinition>): ExtensionCommand;
32
+ /**
33
+ * Strips a single layer of matching outer `"` or `'` quotes from a string,
34
+ * trimming surrounding whitespace. If the input is not wrapped in a matching
35
+ * pair, returns it trimmed but otherwise unchanged.
36
+ */
37
+ export declare function stripQuoteArtifacts(raw: string): string;
38
+ /**
39
+ * Pre-parse repair for a subcommand's raw arg string. Fires when the
40
+ * surrounding command fails to parse (via `buildSubcommandRepair`). Strips
41
+ * any leading/trailing quote, escapes inner single quotes with the POSIX
42
+ * `'\''` idiom, and wraps the result in single quotes so the shell parser
43
+ * will treat the payload as an opaque literal on the next pass.
44
+ *
45
+ * Returns `null` when the repaired arg string would be empty.
46
+ */
47
+ export declare function repairQuotedArg(rawArgs: string): string | null;
48
+ export declare function buildSubcommandRepair(name: string, subcommands: Record<string, SubcommandDefinition>): (raw: string) => string;
49
+ //# sourceMappingURL=subcommand.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subcommand.d.ts","sourceRoot":"","sources":["../../../src/lib/sandbox/subcommand.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAG/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEhF;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IAC5C,OAAO,EAAE,CACP,IAAI,EAAE,MAAM,EAAE,EACd,GAAG,EAAE,uBAAuB,KACzB,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;CAC7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,GAChD,gBAAgB,CAsBlB;AAMD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAUvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK9D;AAED,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,IAO1B,KAAK,MAAM,KAAG,MAAM,CA2B5C"}
@@ -0,0 +1,46 @@
1
+ import type { Tool } from 'ai';
2
+ import type { BashToolkit, CommandResult } from 'bash-tool';
3
+ import type { SkillPathMapping } from '../skills/types.ts';
4
+ import type { FileEvent } from './file-events.ts';
5
+ /**
6
+ * Declarative skill upload: a host directory whose contents are copied into
7
+ * the sandbox at startup. The factory also parses each skill's frontmatter
8
+ * and exposes the result on `sandbox.skills`.
9
+ */
10
+ export interface SkillUploadInput {
11
+ /** Host directory containing skill subdirectories (each with a SKILL.md). */
12
+ host: string;
13
+ /** Destination inside the sandbox (e.g. `/workspace/skills`). */
14
+ sandbox: string;
15
+ }
16
+ /**
17
+ * Input schema exposed by the wrapped bash tool — adds a required `reasoning`
18
+ * field on top of the upstream `{ command }` shape.
19
+ */
20
+ export interface BashToolInput {
21
+ command: string;
22
+ reasoning: string;
23
+ }
24
+ /** The shared wrapper's bash tool type (widened from upstream). */
25
+ export type WrappedBashTool = Tool<BashToolInput, CommandResult>;
26
+ /**
27
+ * A sandbox that owns its skills. The factory uploads files + parses
28
+ * frontmatter once; `skills` is then the single source of truth for
29
+ * the `skills()` fragment. The `bash` tool is widened to require a
30
+ * `reasoning` input on every call.
31
+ */
32
+ export interface AgentSandbox extends Omit<BashToolkit, 'bash' | 'tools'> {
33
+ /** Discovered skills — empty array if none were configured. */
34
+ skills: SkillPathMapping[];
35
+ bash: WrappedBashTool;
36
+ tools: Omit<BashToolkit['tools'], 'bash'> & {
37
+ bash: WrappedBashTool;
38
+ };
39
+ /**
40
+ * Drain and return file events observed since the last call. Attached by
41
+ * callers who composed an `ObservedFs` into their backend; omitted
42
+ * otherwise. The chat pipeline reads this via optional chaining.
43
+ */
44
+ drainFileEvents?(): FileEvent[];
45
+ }
46
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/sandbox/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,IAAI,EAAE,MAAM,CAAC;IACb,iEAAiE;IACjE,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,mEAAmE;AACnE,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,WAAW,YAAa,SAAQ,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC;IACvE,+DAA+D;IAC/D,MAAM,EAAE,gBAAgB,EAAE,CAAC;IAC3B,IAAI,EAAE,eAAe,CAAC;IACtB,KAAK,EAAE,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,GAAG;QAAE,IAAI,EAAE,eAAe,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,eAAe,CAAC,IAAI,SAAS,EAAE,CAAC;CACjC"}
@@ -0,0 +1,14 @@
1
+ import type { Sandbox } from 'bash-tool';
2
+ import type { SkillPathMapping } from '../skills/types.ts';
3
+ import type { SkillUploadInput } from './types.ts';
4
+ /**
5
+ * Discover skills under each host directory, write all non-dotfile, non-symlink
6
+ * files into the sandbox under the mapped path, and return the discovered
7
+ * skill metadata with sandbox paths.
8
+ *
9
+ * Later inputs override earlier ones when the same skill name appears twice.
10
+ * Files with the same sandbox path are overwritten in order, so the last
11
+ * writer wins.
12
+ */
13
+ export declare function uploadSkills(sandbox: Sandbox, inputs: SkillUploadInput[]): Promise<SkillPathMapping[]>;
14
+ //# sourceMappingURL=upload-skills.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-skills.d.ts","sourceRoot":"","sources":["../../../src/lib/sandbox/upload-skills.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIzC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAenD;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,gBAAgB,EAAE,GACzB,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAqC7B"}
@@ -0,0 +1,17 @@
1
+ export interface WalkedFile {
2
+ /** Absolute host path of the file. */
3
+ path: string;
4
+ /** Relative POSIX path from the walk root (suitable for sandbox targets). */
5
+ relativePath: string;
6
+ /** File contents (as Buffer to preserve binary assets). */
7
+ content: Buffer;
8
+ }
9
+ /**
10
+ * Recursively list every regular file under `root`. Skips symlinks (to avoid
11
+ * escaping `root`) and dotfiles / dot-directories (e.g. `.git`, `.DS_Store`,
12
+ * `.env`). Binary contents are preserved.
13
+ *
14
+ * Returns an empty array when `root` does not exist.
15
+ */
16
+ export declare function walkDirectory(root: string): Promise<WalkedFile[]>;
17
+ //# sourceMappingURL=walk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.d.ts","sourceRoot":"","sources":["../../../src/lib/sandbox/walk.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,UAAU;IACzB,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,6EAA6E;IAC7E,YAAY,EAAE,MAAM,CAAC;IACrB,2DAA2D;IAC3D,OAAO,EAAE,MAAM,CAAC;CACjB;AAQD;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAiCvE"}
@@ -1,28 +1,23 @@
1
1
  import type { ContextFragment } from '../fragments.ts';
2
- import type { SkillsFragmentOptions } from './types.ts';
2
+ import type { AgentSandbox } from '../sandbox/types.ts';
3
3
  /**
4
- * Create a context fragment containing available skills metadata.
4
+ * Create a context fragment containing available skills metadata from a
5
+ * sandbox.
5
6
  *
6
7
  * Follows Anthropic's progressive disclosure pattern:
7
8
  * - At startup: only skill metadata (name, description, path) is injected
8
9
  * - At runtime: LLM reads full SKILL.md using file tools when relevant
9
10
  *
10
- * @param options - Configuration including paths to scan and optional filtering
11
+ * @param sandbox - An AgentSandbox whose `.skills` has been populated by the
12
+ * sandbox factory (`createBashTool` / `createContainerTool`).
11
13
  *
12
14
  * @example
13
15
  * ```ts
14
- * const context = new ContextEngine({ userId: 'demo-user', store, chatId: 'demo' })
15
- * .set(
16
- * role('You are a helpful assistant.'),
17
- * skills({
18
- * paths: [
19
- * { host: './skills', sandbox: '/skills/skills' }
20
- * ]
21
- * }),
22
- * );
23
- *
24
- * // LLM now sees skill metadata with sandbox paths and can read full SKILL.md
16
+ * const sandbox = await createBashTool({
17
+ * skills: [{ host: './skills', sandbox: '/skills/skills' }],
18
+ * });
19
+ * context.set(role('You are a helpful assistant.'), skills(sandbox));
25
20
  * ```
26
21
  */
27
- export declare function skills(options: SkillsFragmentOptions): ContextFragment;
22
+ export declare function skills(sandbox: AgentSandbox): ContextFragment;
28
23
  //# sourceMappingURL=fragments.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fragments.d.ts","sourceRoot":"","sources":["../../../src/lib/skills/fragments.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,OAAO,KAAK,EAAiB,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,qBAAqB,GAAG,eAAe,CAmFtE"}
1
+ {"version":3,"file":"fragments.d.ts","sourceRoot":"","sources":["../../../src/lib/skills/fragments.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,YAAY,GAAG,eAAe,CAwB7D"}
@@ -14,8 +14,8 @@ export interface SkillMetadata {
14
14
  skillMdPath: string;
15
15
  }
16
16
  /**
17
- * Individual skill mount with name.
18
- * Output from getSkillMounts().
17
+ * Individual skill mount emitted by sandbox factories and consumed by the
18
+ * `skills()` fragment.
19
19
  */
20
20
  export interface SkillPathMapping {
21
21
  name: string;
@@ -25,31 +25,6 @@ export interface SkillPathMapping {
25
25
  /** Sandbox path to SKILL.md */
26
26
  sandbox: string;
27
27
  }
28
- /**
29
- * Options for the skills() fragment helper.
30
- */
31
- export interface SkillsFragmentOptions {
32
- /**
33
- * Skill directories to scan, with host-to-sandbox path mapping.
34
- *
35
- * @example
36
- * ```ts
37
- * skills({
38
- * paths: [
39
- * { host: 'apps/backend/dist/skills', sandbox: '/skills/skills' }
40
- * ]
41
- * })
42
- * ```
43
- */
44
- paths: {
45
- host: string;
46
- sandbox: string;
47
- }[];
48
- /** Skill names to exclude from the fragment */
49
- exclude?: string[];
50
- /** Skill names to include (if set, only these are included) */
51
- include?: string[];
52
- }
53
28
  /**
54
29
  * Result of parsing a SKILL.md file.
55
30
  */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/skills/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,yCAAyC;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;;;;;;;OAWG;IACH,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC3C,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,yBAAyB;IACzB,WAAW,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/skills/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,yCAAyC;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,yBAAyB;IACzB,WAAW,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -1,5 +1,5 @@
1
1
  import type { UIMessage } from 'ai';
2
- import type { AgentModel } from '@deepagents/agent';
2
+ import type { AgentModel } from './advisor.ts';
3
3
  export declare function staticChatTitle(message: UIMessage): string;
4
4
  export interface GenerateChatTitleOptions {
5
5
  message: UIMessage;
@@ -1 +1 @@
1
- {"version":3,"file":"title.d.ts","sourceRoot":"","sources":["../../src/lib/title.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAGpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AA6BpD,wBAAgB,eAAe,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAE1D;AAED,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,SAAS,CAAC;IACnB,KAAK,EAAE,UAAU,CAAC;IAClB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,MAAM,CAAC,CAyBjB"}
1
+ {"version":3,"file":"title.d.ts","sourceRoot":"","sources":["../../src/lib/title.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAGpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AA4B/C,wBAAgB,eAAe,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAE1D;AAED,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,SAAS,CAAC;IACnB,KAAK,EAAE,UAAU,CAAC;IAClB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,MAAM,CAAC,CAyBjB"}
@@ -1 +1 @@
1
- {"version":3,"file":"exporter.d.ts","sourceRoot":"","sources":["../../../src/lib/tracing/exporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAKzC;AAED,MAAM,WAAW,2BAA2B;IAC1C,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,oBAAqB,YAAW,eAAe;;gBAS9C,OAAO,GAAE,2BAAgC;IAY/C,MAAM,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAgEtE"}
1
+ {"version":3,"file":"exporter.d.ts","sourceRoot":"","sources":["../../../src/lib/tracing/exporter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAKV,SAAS,EAEV,MAAM,YAAY,CAAC;AA6CpB,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAKzC;AAED,MAAM,WAAW,2BAA2B;IAC1C,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,qBAAa,oBAAqB,YAAW,eAAe;;gBAS9C,OAAO,GAAE,2BAAgC;IAY/C,MAAM,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAgEtE"}
@@ -81,6 +81,32 @@ var BatchTraceProcessor = class {
81
81
  };
82
82
 
83
83
  // packages/context/src/lib/tracing/exporter.ts
84
+ var STRINGIFIED_IO_TYPES = {
85
+ function: true,
86
+ transcription: true
87
+ };
88
+ function hasStringifiedIO(data) {
89
+ return data.type in STRINGIFIED_IO_TYPES;
90
+ }
91
+ function encodeField(value) {
92
+ if (value == null) return void 0;
93
+ return typeof value === "string" ? value : JSON.stringify(value);
94
+ }
95
+ function toWireItem(item) {
96
+ if (item.object !== "trace.span") return item;
97
+ const spanData = item.span_data;
98
+ if (!hasStringifiedIO(spanData)) {
99
+ return { ...item, span_data: spanData };
100
+ }
101
+ return {
102
+ ...item,
103
+ span_data: {
104
+ ...spanData,
105
+ input: encodeField(spanData.input),
106
+ output: encodeField(spanData.output)
107
+ }
108
+ };
109
+ }
84
110
  var OpenAIExportError = class extends Error {
85
111
  status;
86
112
  constructor(status, body) {
@@ -125,7 +151,7 @@ var OpenAITracesExporter = class {
125
151
  if (this.#project) {
126
152
  headers["OpenAI-Project"] = this.#project;
127
153
  }
128
- const body = JSON.stringify({ data: items });
154
+ const body = JSON.stringify({ data: items.map(toWireItem) });
129
155
  await this.#fetchWithRetry(this.#endpoint, {
130
156
  method: "POST",
131
157
  headers,
@@ -304,7 +330,6 @@ function normalizeUsage(usage) {
304
330
  return {
305
331
  input_tokens: usage.inputTokens,
306
332
  output_tokens: usage.outputTokens,
307
- total_tokens: usage.totalTokens,
308
333
  ...Object.keys(details).length > 0 ? { details } : {}
309
334
  };
310
335
  }
@@ -547,8 +572,8 @@ function createOpenAITracesIntegration(options = {}) {
547
572
  if (openRuns.length === 1) {
548
573
  return openRuns[0];
549
574
  }
550
- let bestState;
551
575
  let bestScore = 0;
576
+ let bestStates = [];
552
577
  const eventMetadataRef = asObjectRef(event.metadata);
553
578
  const eventContextRef = asObjectRef(event.experimental_context);
554
579
  const eventModelKey = event.model != null ? toModelKey(event.model) : void 0;
@@ -579,10 +604,22 @@ function createOpenAITracesIntegration(options = {}) {
579
604
  }
580
605
  if (score > bestScore) {
581
606
  bestScore = score;
582
- bestState = state;
607
+ bestStates = [state];
608
+ } else if (score === bestScore && bestScore > 0) {
609
+ bestStates.push(state);
583
610
  }
584
611
  }
585
- return bestState;
612
+ if (bestStates.length === 0) return void 0;
613
+ if (bestStates.length === 1) return bestStates[0];
614
+ if (event.response?.id != null) {
615
+ const withOpenStep = bestStates.filter((s) => s.stepSpans.length > 0);
616
+ if (withOpenStep.length >= 1) return withOpenStep[0];
617
+ }
618
+ if (event.toolCall?.toolCallId != null) {
619
+ const withOpenTool = bestStates.filter((s) => s.toolCallSpans.size > 0);
620
+ if (withOpenTool.length >= 1) return withOpenTool[0];
621
+ }
622
+ return bestStates[0];
586
623
  }
587
624
  function closeRunState(state) {
588
625
  const index = openRuns.indexOf(state);
@@ -628,16 +665,26 @@ function now() {
628
665
  function toModelKey(model) {
629
666
  return `${model.provider}:${model.modelId}`;
630
667
  }
668
+ function canonicalizeMessageContent(content) {
669
+ if (typeof content === "string") {
670
+ return [{ type: "text", text: content }];
671
+ }
672
+ return content;
673
+ }
674
+ function canonicalizeMessages(messages) {
675
+ if (!Array.isArray(messages)) return messages;
676
+ return messages.map((message) => {
677
+ if (message == null || typeof message !== "object") return message;
678
+ const record = message;
679
+ if (!("content" in record)) return record;
680
+ return { ...record, content: canonicalizeMessageContent(record.content) };
681
+ });
682
+ }
631
683
  function fingerprintInput(input) {
632
- const normalizedMessages = input.messages ?? (typeof input.prompt === "string" ? [
633
- {
634
- role: "user",
635
- content: [{ type: "text", text: input.prompt }]
636
- }
637
- ] : input.prompt);
684
+ const source = input.messages ?? (typeof input.prompt === "string" ? [{ role: "user", content: input.prompt }] : input.prompt);
638
685
  const normalized = normalizeForJson({
639
686
  system: input.system,
640
- messages: normalizedMessages
687
+ messages: canonicalizeMessages(source)
641
688
  });
642
689
  if (normalized == null) {
643
690
  return void 0;