Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions sdks/ts/packages/golem-ts-sdk/src/baseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AgentTypeRegistry } from './internal/registry/agentTypeRegistry';
import { AgentClassName } from './agentClassName';
import { Datetime } from 'golem:rpc/types@0.2.2';
import { Uuid } from 'golem:agent/host';
import { getAgentId } from './internal/registry/agentId';

/**
* BaseAgent is the foundational class for defining agent implementations.
Expand Down Expand Up @@ -51,10 +52,16 @@ export class BaseAgent {
* @throws Will throw if accessed before the agent is initialized.
*/
getId(): AgentId {
throw new Error(
`AgentId is not available for \`${this.constructor.name}\`. ` +
`Ensure the class is decorated with @agent()`,
);
const agentId = getAgentId();

if (!agentId) {
throw new Error(
`AgentId is not available for \`${this.constructor.name}\`. ` +
`Ensure the class is decorated with @agent()`,
);
}

return agentId;
}

/**
Expand Down
1 change: 1 addition & 0 deletions sdks/ts/packages/golem-ts-sdk/src/decorators/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export function agent(options?: AgentDecoratorOptions) {
const instance = new ctor(...deserializedConstructorArgs.val);

const agentId = getRawSelfAgentId();

if (!agentId.value.startsWith(agentTypeName.asWit)) {
const error = createCustomError(
`Expected the container name in which the agent is initiated to start with "${agentTypeName.asWit}", got "${agentId.value}"`,
Expand Down
3 changes: 3 additions & 0 deletions sdks/ts/packages/golem-ts-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AgentTypeRegistry } from './internal/registry/agentTypeRegistry';
import { AgentInitiatorRegistry } from './internal/registry/agentInitiatorRegistry';
import { getRawSelfAgentId } from './host/hostapi';
import { AgentInitiator } from './internal/agentInitiator';
import { setAgentId } from './internal/registry/agentId';

export { BaseAgent } from './baseAgent';
export { AgentId } from './agentId';
Expand Down Expand Up @@ -68,6 +69,8 @@ async function initialize(
);
}

setAgentId(getRawSelfAgentId());

const initiateResult = initiator.initiate(input, principal);

if (initiateResult.tag === 'ok') {
Expand Down
25 changes: 25 additions & 0 deletions sdks/ts/packages/golem-ts-sdk/src/internal/registry/agentId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Golem Source License v1.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { AgentId } from '../../agentId';

let agentId: AgentId | null = null;

export function setAgentId(id: AgentId): void {
agentId = id;
}

export function getAgentId(): AgentId | null {
return agentId;
}
Loading